HTML5 Boilerplate和jQuery [英] HTML5 Boilerplate and jQuery

查看:167
本文介绍了HTML5 Boilerplate和jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么来自 http://html5boilerplate.com/ 的样板在网络内容之后声明了jQuery?这有充分的理由吗?

I was wondering why the boilerplate from http://html5boilerplate.com/ declare jQuery after web content? Is there are a good reason for this?

这是代码的片段...

This is a snippet of the code...


  <!-- Add your site or application content here -->
    <p>Hello world! This is HTML5 Boilerplate.</p>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.9.0.min.js"><\/script>')</script>
    <script src="js/plugins.js"></script>
    <script src="js/main.js"></script>


P.S。什么 window.jQuery || part do?

P.S. what does window.jQuery || part do?

推荐答案

它检查如果正确加载CDN副本。如果没有,它会加载本地副本。

It checks if the CDN copy loaded correctly. If not, it loads a local copy.

当jQuery在页面上运行时,它会创建一个全局 jQuery 变量。这也可以作为全局对象的属性访问: window.jQuery 。如果jQuery没有运行, window.jQuery 将是 undefined

When jQuery runs on the page, it creates a global jQuery variable. This can also be accessed as a property of the global object: window.jQuery. If jQuery hasn't run, window.jQuery will be undefined.

|| 是以下的简写版本:

if ( window.jQuery == undefined ) {
    document.write('<script src="js/vendor/jquery-1.9.0.min.js"><\/script>');
}

这样,如果谷歌的CDN关闭(或浏览器无法访问 ajax.googleapis.com )您的网站不会中断。相反,将从您的域中提供相同的jQuery副本。

This way, if Google's CDN is down (or if the browser cannot access ajax.googleapis.com) your site doesn't break. Instead, an identical copy of jQuery will be served from your domain.

它位于底部的原因是因为那是什么 Yahoo的性能指南建议:

The reason it's at the bottom is because that's what Yahoo's performance guide recommends:


脚本引起的问题是阻止并行下载。 HTTP / 1.1规范建议浏览器每个主机名并行下载不超过两个组件。如果您从多个主机名提供图像,则可以并行执行两次以上的下载。但是,在下载脚本时,即使在不同的主机名上,浏览器也不会启动任何其他下载。

The problem caused by scripts is that they block parallel downloads. The HTTP/1.1 specification suggests that browsers download no more than two components in parallel per hostname. If you serve your images from multiple hostnames, you can get more than two downloads to occur in parallel. While a script is downloading, however, the browser won't start any other downloads, even on different hostnames.

[...]

如果可以延迟脚本,也可以移动到页面底部。这将使您的网页加载速度更快

If a script can be deferred, it can also be moved to the bottom of the page. That will make your web pages load faster.

有关详细信息,请参阅Steve Souders的关于此主题的优秀文章

For more info on this, refer to Steve Souders' excellent article on this topic.

这篇关于HTML5 Boilerplate和jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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