我在桌面上有一个index.html
文件,我想获得具有以下选项的jQuery库:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js></script>
但看起来对本地文件不起作用。
哪里出了问题?
发布于 2015-08-25 14:42:27
它不能正常工作的原因是,使用//
启动URL将使用“当前”协议。在internet上,您可以使用HTTP
或HTTPS
协议访问资源。但是当显示来自本地硬盘的文件时,所使用的协议是FILE
,所以不是转换到想要的http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js
,而是转换为file://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js
。
因此,这里的选项是使用javascript加载脚本,如下面所示,或者硬编码协议。
<script>
var p = 'http';
if (document.location.protocol == "https:"){
p+='s';
}
var z = document.createElement("script");
z.type = "text/javascript";
z.src = p + "://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(z, s);
</script>
这将查看当前的协议,因此它使用正确的协议。
PS:我使用了SO源代码中的adzerk代码,并对其进行了修改.我知道Google分析使用的是同样的片段。
https://stackoverflow.com/questions/32205838
复制相似问题