yepnope.js 资源回退不起作用

发布于 2024-11-02 21:50:43 字数 510 浏览 2 评论 0原文

我直接使用 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 没有被加载。

由于我没有连接到互联网,我假设在上面的示例中 Google CDN 版本将无法加载,因此将调用 complete 函数来加载我的本地副本。看起来 complete 根本没有被调用,因为我在控制台中没有看到“made it”。

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

I used the code straight from the examples on yepnope's homepage:

  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'); }
    }
  }]);

I've been working without internet today and I noticed that my local version of jQuery is not being loaded.

Since I'm not connected to the internet I would assume in the example above the Google CDN version would fail to load, the complete function would be called which would load my local copy. It looks like complete is not being called at all because I am not seeing 'made it' in the console.

Also, I checked and the path to my local copy is correct.

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

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

发布评论

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

评论(1

萌化 2024-11-09 21:50:43

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

您必须等待它超时。完整的函数不会立即触发。我刚刚下载了 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 秒后,在控制台中,“回调”和“完成”消息都会显示,所以我知道他们将被解雇。

替代方案:

如果您发现您只需要此功能来进行在线/离线开发,您可以尝试 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>

EDIT based on your comments and question update:

You have to wait for it to time out. The complete function won't fire immediately. I just downloaded yepnope.js and ran their demo/index.html with the following code added just under their yepnope call that loads jQuery at the bottom of the page:

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"); }

});

Obviously jQuery 1.6.2 will not load. About 10-15 seconds later in the console, both the "callback" and "complete" messages show up, so I know they are getting fired.

Alernative:

If you find that you are only needing this functionality for developing online/offline, you might try what Html5Boilerplate uses, which I have adpoted:

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

Here's what I personally use:

    </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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文