我应该如何从安全区域的网页引用我的 css 文件(位于非安全区域)。我考虑过复制它(将其中一个移到安全区域),但这似乎效率很低。
非常感谢任何建议。 (p.s. 后面多半会有几个问题哈哈)
最佳答案
您始终可以通过使用相对/根路径来避免此问题:
<link rel="stylesheet" href="/css/screen.css">
如果您必须使用完整的 URL,我不确定为什么您不能使用 https 协议(protocol)(这是正确的解决方案),但还有一个选项:不指定一个协议(protocol)。
<link rel="stylesheet" href="//example.com/css/screen.css">
http://paulirish.com/2010/the-protocol-relative-url/
If the browser is viewing that current page in through HTTPS, then it'll request that asset with the HTTPS protocol, otherwise it'll typically* request it with HTTP. This prevents that awful "This Page Contains Both Secure and Non-Secure Items" error message in IE, keeping all your asset requests within the same protocol.
但是:
Caveat: When used on a
<link>
or @import for a stylesheet, IE7 and IE8 download the file twice. All other uses, however, are just fine.
因此,如果您必须指定完整的 URL,最好/正确的方法是:
<link rel="stylesheet" href="https://example.com/css/screen.css">
真的别无选择。 CSS 文件本身中图像和资源的相对路径应该适用于任何一种方法,并且不会触发安全错误。如果您需要 CSS 文件中的绝对 URL,则可以使用相同的技巧。
关于css - HTTPS 以及如何引用文件和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8019527/