我正在向我的页眉和页脚添加一个外部CSS文件和一个外部JS文件。加载HTTPS页面时,一些浏览器会抱怨我加载的是不安全的内容。
当页面本身是HTTPS时,有没有简单的方法让浏览器通过HTTPS加载外部内容?
发布于 2010-04-28 06:22:34
使用协议相对路径。
因此不会
<link rel="stylesheet" href="http://example.com/style.css">
<script src="http://example.com/script.js"></script>
但就是这样
<link rel="stylesheet" href="//example.com/style.css">
<script src="//example.com/script.js"></script>
然后它将使用父页面的协议。
发布于 2010-04-28 06:54:55
纽特和詹姆斯·韦斯特盖特在评论后一个答案时是对的。
如果我们看一看杂乱无章的工业级外部javascript包含,成功的使用了document.location.protocol嗅探和脚本元素注入并使用了适当的协议。
所以你可以使用类似这样的东西:
<script type="text/javascript">
var protocol = (
("https:" == document.location.protocol)
? "https"
: "http"
);
document.write(
unescape(
"%3Cscript"
+ " src='"
+ protocol
+ "://"
+ "your.domain.tld"
+ "/your/script.js"
+ "'"
+ " type='text/javascript'
+ "%3E"
+ "%3C/script%3E"
) // this HAS to be escaped, otherwise it would
// close the actual (not injected) <script> element
);
</script>
对于外部CSS包含也可以这样做。
顺便说一句:注意在CSS中只使用相对url()路径,否则可能仍然会收到"mixed secure And unsecure“警告。
发布于 2010-04-28 06:11:20
如果使用相对路径(并且内容在同一个域中),则内容应使用请求页面时使用的任何协议。
但是,如果要跨域访问CDN或资源站点,或者需要使用绝对路径,则需要使用服务器端脚本更改链接,或者始终使用https://
https://stackoverflow.com/questions/2725523
复制相似问题