js资源回退不工作

yepnope.js resource fallbacks not working

本文关键字:工作 回退 资源 js      更新时间:2023-09-26

我直接使用了yepnope主页上的示例代码:

  yepnope([{
    load: 'http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js',
    complete: function() {
      console.log('made it');
      if(!window.jQuery) { yepnope('/js/jquery.1.5.2-min.js'); }
    }
  }]);

我今天一直在没有互联网的情况下工作,我注意到我的本地版本的jQuery没有被加载。

因为我没有连接到互联网,我假设在上面的例子中,谷歌CDN版本将无法加载,complete函数将被调用,这将加载我的本地副本。看起来complete根本没有被调用,因为我在控制台中没有看到"made It"。

另外,我检查了本地副本的路径是否正确。

根据您的评论和问题编辑更新:

你必须等待它超时。完整的函数不会立即启动。我只是下载了yepnope.js并运行了他们的demo/index.html,并在yepnope调用下添加了以下代码,该调用会在页面底部加载jQuery:

yepnope({
  load     : "http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js",
  callback : function() { console.log("callback"); },
  complete : function() { console.log("complete"); }
});

显然jQuery 1.6.2无法加载。大约10-15秒后,在控制台中,"callback"answers"complete"消息都出现了,所以我知道它们被解雇了。

Alernative:

如果你发现你只需要这个功能来开发在线/离线,你可以尝试Html5Boilerplate使用的,我已经采用了:

<!-- Grab Google CDN's jQuery, with a protocol relative URL; 
     fall back to local if necessary -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" 
        type="text/javascript"></script>
<script type="text/javascript">
    window.jQuery || document.write('<script src="js/jquery-1.5.2.js">'x3C/script>')
</script>

这是我个人使用的:

    </form>
    <!-- Javascript at the bottom for fast page loading -->
    <!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if necessary -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript"> window.jQuery || document.write('<script src="js/jquery-1.5.2.js">'x3C/script>')</script>
    <!-- Grab Google CDN's jQuery UI, with a protocol relative URL; fall back to local if necessary -->
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js" type="text/javascript"></script>
    <script type="text/javascript"> $.ui || document.write('<script src="js/jquery-ui-1.8.4.custom.min.js">'x3C/script>')</script>
    <!-- Scripts concatenated and minified via ant build script-->
    <script src="js/plugins.js" type="text/javascript"></script>
    <script src="js/script.js" type="text/javascript"></script>
    <!-- End scripts -->
</body>
</html>