3

我直接使用了 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根本没有被调用,因为我在控制台中没有看到“成功”。

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

4

1 回答 1

5

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

您必须等待它超时。完整的功能不会立即触发。我刚刚下载了 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>
于 2011-04-22T16:31:37.933 回答