Javascript document.write回退导致jQuery无序加载

Javascript document.write回退导致jQuery无序加载,javascript,jquery,html5boilerplate,Javascript,Jquery,Html5boilerplate,我正在使用HTML5样板文件4.0构建一个新站点,但在jQuery本地回退代码方面遇到了麻烦。有关守则如下: <!-- <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

我正在使用HTML5样板文件4.0构建一个新站点,但在jQuery本地回退代码方面遇到了麻烦。有关守则如下:

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

window.jQuery | | document.write(“”)
我现在正在本地开发,所以我已经注释掉了CDN产品线。我的问题是jQuery确实会加载,但它会在plugins.js和main.js之后加载,从而导致未定义的错误

我发现的最接近可能的解释是#4点,这表明这是意料之中的,但是。。。上面的代码是jQuery最常用的本地回退代码,它是经过严格审查的H5BP。我一定是遗漏了什么,是吗?

不久前我问了一个类似的问题

您可以这样做:

function loadScript(pathToScript, callback) {

    if (/jquery/.test(pathToScript) && window.jQuery) {
        //jQuery has already been loaded so simply call the callback
        callback.apply();

    } else {
        var head = document.getElementsByTagName("head")[0];
        var script = document.createElement("script");

        script.type = "text/javascript";
        script.src = pathToScript + "?t=" + new Date().getTime(); //prevent caching

        if (callback) {
            script.onload = callback;
        }

        head.appendChild(script);
    }
}

var scripts = ['js/vendor/jquery-1.8.1.min.js', 'js/plugins.js', 'js/main.js'];

(function (i) {
    if (i < scripts.length) {
        var self = arguments.callee;

        loadResource(scripts[i], function () {
            self(++i);
        });
    }
})(0);
函数加载脚本(pathToScript,回调){
if(/jquery/.test(pathToScript)和&window.jquery){
//jQuery已经加载,因此只需调用回调即可
callback.apply();
}否则{
var head=document.getElementsByTagName(“head”)[0];
var script=document.createElement(“脚本”);
script.type=“text/javascript”;
script.src=pathToScript+“?t=“+new Date().getTime();//防止缓存
如果(回调){
script.onload=回调;
}
head.appendChild(脚本);
}
}
var scripts=['js/vendor/jquery-1.8.1.min.js','js/plugins.js','js/main.js'];
(职能(一){
if(i
不久前我问了一个类似的问题

您可以这样做:

function loadScript(pathToScript, callback) {

    if (/jquery/.test(pathToScript) && window.jQuery) {
        //jQuery has already been loaded so simply call the callback
        callback.apply();

    } else {
        var head = document.getElementsByTagName("head")[0];
        var script = document.createElement("script");

        script.type = "text/javascript";
        script.src = pathToScript + "?t=" + new Date().getTime(); //prevent caching

        if (callback) {
            script.onload = callback;
        }

        head.appendChild(script);
    }
}

var scripts = ['js/vendor/jquery-1.8.1.min.js', 'js/plugins.js', 'js/main.js'];

(function (i) {
    if (i < scripts.length) {
        var self = arguments.callee;

        loadResource(scripts[i], function () {
            self(++i);
        });
    }
})(0);
函数加载脚本(pathToScript,回调){
if(/jquery/.test(pathToScript)和&window.jquery){
//jQuery已经加载,因此只需调用回调即可
callback.apply();
}否则{
var head=document.getElementsByTagName(“head”)[0];
var script=document.createElement(“脚本”);
script.type=“text/javascript”;
script.src=pathToScript+“?t=“+new Date().getTime();//防止缓存
如果(回调){
script.onload=回调;
}
head.appendChild(脚本);
}
}
var scripts=['js/vendor/jquery-1.8.1.min.js','js/plugins.js','js/main.js'];
(职能(一){
if(i
关于StackOverflow这个问题,我也没有找到一个准确的答案,但是,这一页似乎涵盖了以下主题:

总之,创建真正强健的故障切换解决方案并不简单。我们需要考虑浏览器不兼容性、文档状态和事件、依赖关系、延迟加载和超时!谢天谢地,像LABjs这样的工具让我们完全控制JavaScript文件的加载,从而帮助我们减轻痛苦


注意:解决方案依赖于LABjs的使用,我也没有找到有关StackOverflow的关于此问题的准确答案,但是,似乎本页涵盖了以下主题:

总之,创建真正强健的故障切换解决方案并不简单。我们需要考虑浏览器不兼容性、文档状态和事件、依赖关系、延迟加载和超时!谢天谢地,像LABjs这样的工具让我们完全控制JavaScript文件的加载,从而帮助我们减轻痛苦

<>注释:解决方案依赖于使用Labjs

< P>。您可以考虑使用与ReaveSebug并行加载脚本。

从他们的网站:

js能够进行资源回退,并且 与第一个脚本并行下载相关脚本

以及守则:

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();
    });
  }
}]);
我希望这有帮助

> P>你可以考虑使用你的脚本与后退并行加载。

从他们的网站:

js能够进行资源回退,并且 与第一个脚本并行下载相关脚本

以及守则:

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();
    });
  }
}]);
我希望这有帮助