加载 DOM 后如何运行 jQuery 的后备副本?

发布于 2024-12-11 04:38:32 字数 672 浏览 0 评论 0原文

以下是我文档中 body 结束标记上方

if(!window.jQuery){
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = '/js/jquery.js';
    var scriptHook = document.getElementsByTagName('script')[0];
    scriptHook.parentNode.insertBefore(script, scriptHook);
}


jQuery(document).ready(function($){
// page behaviors
});

它确实执行成功,因为如果我的计算机未连接到 Internet(这是本地提供的页面),则会插入 jQuery 的本地副本。但是,下面的 document.ready() 部分不会执行。我猜这是因为它是在 jQuery 的后备副本生效之前调用的。以某种方式“延迟”其执行以便 jQuery 的任一副本都能正常工作的正确做法是什么?

The following are the first lines of code in a <script> tag just above the closing body tag in my document (it specifies that a locally-served copy of jQuery is run in the event that Google's CDN fails):

if(!window.jQuery){
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = '/js/jquery.js';
    var scriptHook = document.getElementsByTagName('script')[0];
    scriptHook.parentNode.insertBefore(script, scriptHook);
}


jQuery(document).ready(function($){
// page behaviors
});

It does execute successfully, in the sense that if my computer is not connected to the Internet (this is a locally-served page), the local copy of jQuery is inserted. However, the document.ready() section below does not execute. I'm guessing this is because it is invoked before the fallback copy of jQuery takes effect. What's the proper practice for somehow "delaying" its execution so that either copy of jQuery will work properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

攒一口袋星星 2024-12-18 04:38:32

考虑使用现有的脚本加载器,例如 yepnope。主页上有一个示例说明了您正在尝试执行的操作。

Consider using an existing script loader such as yepnope. There's an example of exactly what you're trying to do on the home page.

灵芸 2024-12-18 04:38:32

在调用 jQuery 之前,您需要确保附加到 dom 的脚本已完成加载。您可以使用此处描述的技术来完成此操作:

if(!window.jQuery){
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = '/js/jquery.js';
    script.onreadystatechange= function () {
      if (this.readyState == 'complete') jQueryLoaded();
    }
    script.onload = jQueryLoaded;
    var scriptHook = document.getElementsByTagName('script')[0];
    scriptHook.parentNode.insertBefore(script, scriptHook);
}

function jQueryLoaded() {  };

您还可以将 jQuery 内容作为 Ajax 请求获取,创建一个脚本标签,将其作为脚本的主体并附加它。那也行。

You need to be sure that the script you are appending to the dom has finished loading before calling jQuery. You can do this with the technique described here:

if(!window.jQuery){
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = '/js/jquery.js';
    script.onreadystatechange= function () {
      if (this.readyState == 'complete') jQueryLoaded();
    }
    script.onload = jQueryLoaded;
    var scriptHook = document.getElementsByTagName('script')[0];
    scriptHook.parentNode.insertBefore(script, scriptHook);
}

function jQueryLoaded() {  };

You can also fetch the jQuery contents as an Ajax request, create a script tag with those as the body of the script and append it. That would also work.

她如夕阳 2024-12-18 04:38:32

尝试一下

<script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.2.min.js"><\/script>')</script>
<script>
    jQuery(document).ready(function($){
        // page behaviors
    });
</script>

这样脚本标签将被同步加载。

Try that

<script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.2.min.js"><\/script>')</script>
<script>
    jQuery(document).ready(function($){
        // page behaviors
    });
</script>

This way the script tag will be loaded synchronously.

倾`听者〃 2024-12-18 04:38:32

最近似乎多次出现“我如何应对 CDN 故障并加载服务器上托管的文件”的问题。

我要问的问题是,添加更多 js 是否是实现弹性的方法,以及 js 方法真正添加的弹性级别是多少,例如,如果 CDN 出现故障,它们将很快失败,但是如果CDN 响应缓慢 这些解决方案的应对能力如何?

解决此问题的另一种方法是将其视为基础设施问题...

根据您拥有的域/子域运行 CDN。自动监控其可用性,当 DNS 失败时,将其切换到备份服务器(任播也可以提供替代解决方案)

The question "of how do I cope with my CDN failing and load a file hosted on my server" seems to come up a few times lately.

Question I'd ask is whether adding yet more js is the way to achieve the resilience and what level of resilience do the js approaches really add e.g. if the CDN is down they'll be a quick failure but how well do these approaches if the CDN is slow to respond how well do these solutions cope?

An alternative way to approach this is treat it as an infrastructure problem...

Run a CDN based on a domain/sub-domain you own. Have automated monitoring on it's availability, when it fails switch the DNS over to a backup server (anycast may provide an alternative solution too)

千笙结 2024-12-18 04:38:32

php 解决方案将是这样的:

$google_jquery = 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js';
$fp = @fsockopen($google_jquery, 'r');
if (!$fp) 
{
    echo '<script type="text/javascript" src="js/jquery.js"></script>';
} 
else
{ 
    echo '<script src="'.$google_jquery.'"></script>' }
}

A php solution would be something like this:

$google_jquery = 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js';
$fp = @fsockopen($google_jquery, 'r');
if (!$fp) 
{
    echo '<script type="text/javascript" src="js/jquery.js"></script>';
} 
else
{ 
    echo '<script src="'.$google_jquery.'"></script>' }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文