为了获得更好的效果,通过jQuery
引用jQuery CDN
文件是否更好?
像
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
或者
在我们的项目中引用存储的jQuery
文件是否更好?
像
<script type="text/javascript" src="/js/jQuery.min.js"></script>
由于jQuery CDN
链接处理缓存,它是否比我们项目中包含的jQuery
文件提高了性能?
答案 0 :(得分:6)
使用后备
这样做<script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="/libs/js/jquery-3.2.1.js"><\/script>')</script>
CDN优先,如果没有(或阻止)?所以从你的服务器带来它
Why should I use Google's CDN for jQuery?(对我来说,我的意思是缓存命中)
答案 1 :(得分:4)
第一个变种。因为谷歌拥有最好的全球服务器。
启动jQuery引擎的最佳方式:
<script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1.10.2");
google.setOnLoadCallback(function() {
// Place init code here instead of $(document).ready()
});
</script>
这是一篇很好的文章:
http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/
答案 2 :(得分:1)
使用CDN:
如果用户已在其他网站上下载该文件,则该文件已在缓存中
如果用户没有该文件,该文件将始终位于离访问者最近的服务器上
此外,您将减少服务器上的负载,从而使其更快地呈现网站