document.write fallback导致jQuery无序加载 [英] document.write fallback causing jQuery to load out of order

查看:117
本文介绍了document.write fallback导致jQuery无序加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HTML5 Boilerplate 4.0构建一个新站点,并且遇到了jQuery本地回退代码的问题。有问题的代码在这里:

I'm building a new site using HTML5 Boilerplate 4.0, and am running into trouble with its jQuery local fallback code. The code in question is here:

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

我现在正在本地开发,所以我已经注释掉了CDN系列。我的问题是jQuery确实加载了,但它在plugins.js和main.js之后加载,导致未定义的错误。

I'm developing locally, for now, so I've commented out the CDN line. My problem is that jQuery does load, but it loads after plugins.js and main.js, leading to undefined errors.

最接近我发现的解释是之前的答案的第4点,这表明这是预期的,但......以上很容易最常用的jQuery本地回退代码,它是H5BP,经过严格审查。我必须遗漏一些东西,是吗?

The closest to maybe an explanation I've found is the #4 point of this previous answer, which suggests this would be expected, but... the above is easily the most used local fallback code for jQuery, and it's H5BP, which is heavily vetted. I must be missing something, yes?

推荐答案

您可以考虑使用 yepnope 与后备并行加载你的脚本。

You may consider using yepnope to load your scripts in parallel with the fallback.

从他们的网站:


yepnope.js能够执行资源回退,并且仍然与第一个并行的
下载相关脚本。

yepnope.js has the capability to do resource fallbacks and still download dependent scripts in parallel with the first.

代码:

yepnope([{
  load: 'http:/­/ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js',
  complete: function () {
    if (!window.jQuery) {
      yepnope('local/jquery.min.js');
    }
  }
}, {
  load: 'jquery.plugin.js',
  complete: function () {
    jQuery(function () {
      jQuery('div').plugin();
    });
  }
}]);

我希望这有帮助!

这篇关于document.write fallback导致jQuery无序加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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