如果尚未加载jquery,请加载它;如果没有可用的连接,请使用本地版本 [英] Load jquery if its not already loaded and use local version if no connection is available

查看:94
本文介绍了如果尚未加载jquery,请加载它;如果没有可用的连接,请使用本地版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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>

这是我当前的代码.如果script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"失败,是否可以加载jquery lib的本地副本?并且此脚本是否异步加载?还是会冻结ui线程直到被加载?

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? And is this script loaded asynchronous? Or will it freeze the ui thread until loaded?

推荐答案

您可以执行以下操作:

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

这篇关于如果尚未加载jquery,请加载它;如果没有可用的连接,请使用本地版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆