简体   繁体   English

如果尚未加载jquery,请加载它;如果没有可用的连接,请使用本地版本

[英]Load jquery if its not already loaded and use local version if no connection is available

<script type="text/javascript">
if (typeof jQuery == 'undefined') {
var script = document.createElement('script');
script.type = "text/javascript";
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(script);
}
</script>

Here is my current code. 这是我当前的代码。 Is there any way to load a local copy of the jquery lib, if script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" fails? 如果script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"失败,是否可以加载jquery lib的本地副本? And is this script loaded asynchronous? 并且此脚本是否异步加载? Or will it freeze the ui thread until loaded? 还是会冻结ui线程直到加载?

You can just do this: 您可以这样做:

<script src="cdn.jquery.js"></script>
<script>window.jQuery || document.write('<script src="/local/jquery.js"><\/script>')</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM