在CDN关闭时加载jQuery的备份副本 [英] loading a backup copy of jQuery when CDN is down

查看:99
本文介绍了在CDN关闭时加载jQuery的备份副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用于初始化所有应用程序的脚本中包含此代码,它将google CDN中的jQuery加载到我们所有应用程序所需的其他几个内容中。然后当我们加载特定的程序功能时,我们检查以确保jquery已加载,以防CDN关闭。我遇到的问题是它仍在加载第二个。如果我在行 headTag.appendChild(jqTag); 之后添加一个简单的警报(测试); 完美,但如果我删除它使用第二个警报。给出了什么?

I have this code in a script we use for initializing all of our applications, it loads the jQuery from the google CDN amongst several other things that all of our applications require. Then when we load the specific program functionality we check to make sure that jquery has loaded, in case the CDN is down. The problem I am running into is it is still loading the second one. If I add a simple alert("Test"); after the line headTag.appendChild(jqTag); it works perfectly, but if I remove the alert it uses the second one. What gives?

它们的加载如下:

<script type="text/javascript" src="i-initializer.js"></script>
<script type="text/javascript" src="i-program.js"></script>

初始化程序脚本:

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

然后在另一个脚本中我们有以下内容:

Then in another script we have the following:

if(typeof jQuery=='undefined'){
    var header = document.getElementsByTagName("head")[0];
    var qtag = document.createElement('script');
    qtag.type = 'text/javascript';
    qtag.src = 'http://feedback.oursite.com/scripts/jquery-1.8.3.min.js';
    qtag.onload = checkjQueryUI;
    header.appendChild(qtag);
}
else
{
    jQCode();
}
jQCode() {
...
}


推荐答案

这是 HTML5 Boilerplate 使用的技术。首先,它加载Google CDN脚本,然后立即检查全局 jQuery 对象是否存在 - 如果不存在,则CDN失败并加载本地副本。

This is the technique used by HTML5 Boilerplate. First it loads the Google CDN script, then immediately checks if the global jQuery object exists -- if it doesn't, the CDN failed and a local copy is loaded instead.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.8.3.min.js"><\/script>')</script>

这篇关于在CDN关闭时加载jQuery的备份副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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