Zepto 回退到 jQuery
我在我的 Web 应用程序中使用 ZeptoJS,但如果浏览器不支持 Zepto,我想回退到 jQuery。由于 IE 是目前唯一不支持的主要浏览器,因此我很想检测 IE:
if(navigator.appName == 'Microsoft Internet Explorer'){
// load jquery
} else {
// load zepto
}
但我更愿意专门检测 Zepto 支持并在其他情况下使用 jQuery。有没有一种特征检测方法可以做到这一点?
I'm using ZeptoJS for my web app, but I'd like to fall back to jQuery if the browser doesn't support Zepto. Since IE is the only major browser not supported at the moment, I'm tempted to detect IE:
if(navigator.appName == 'Microsoft Internet Explorer'){
// load jquery
} else {
// load zepto
}
but I'd prefer to specificly detect Zepto support and use jQuery in other cases. Is there a feature detection way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您还可以使用此处 检测浏览器是否为IE,并使用现代异步脚本加载库来加载所需的lib。 Yepnope 示例:
You can also use JS trick described here to detect whether browser is IE, and use a modern asynchronous script loading library to load the required lib. Yepnope example:
我不会使用 Javascript 来实现这一点,而是先行一步并使用条件语句。这可能看起来像:
这直接进入您的 HTML 文件。如果浏览器是 Internet Explorer 7 及更低版本,上面的代码片段将加载 jQuery。否则它将包含 zepto.js。
Rather than doing that with Javascript, I'd take it one step ahead and use conditional statements. This could look like:
This goes right into your HTML files. The above snippet will load jQuery, if the browser is Internet Explorer 7 and below. Otherwise it'll include zepto.js.
正如 Zepto 文档所述,如果您需要检测 Internet Explorer,您可以使用此代码:
Zepto 使用它来依靠 jQuery,但我也使用它作为浏览器检测。
As Zepto Documentation said, if you need to detect Internet Explorer you can use this code:
Zepto use it to fall back on jQuery, but I have use it as browser detection too.
这可能是一个疯狂的想法(我不确定 Zepto 是否会在不受支持的浏览器上加载),但是使用 Zepto 自己的浏览器检测来查看它是否在不受支持的浏览器上怎么样?
也许你可以这样做:
这不会捕获 chrome/firefox,它与 Zepto 一起工作得很好,但它确实符合 Zepto 团队的意图,这可能会更好,也可能不会更好。
This might be a crazy idea (I'm not sure if Zepto will even load on an unsupported browser), but what about using Zepto's own browser detection to see if it's on an unsupported browser?
Maybe you could do something like this:
This won't catch chrome/firefox, which work fine with Zepto, but it does match the Zepto team's intentions for the thing, which may or may not be better.
不要使用条件注释,IE10 不支持它。这是 zepto 文档 中推荐的方法:
在现代浏览器上加载 Zepto,在 IE 上加载 jQuery
Zepto 在 IE 中不起作用,因为 IE 不支持 prototype,因此这正是正确的检查方法。
上面的脚本执行动态加载,但逻辑是
Don't use the conditional comments, it's not going to be supported by IE10. This is the recommended approach from the zepto documentation:
Load Zepto on modern browser and jQuery on IE
Zepto doesn't work in IE because IE doesn't support prototype, so this is exactly the right way to check.
The script above do a dynamical load but the logic is
这是 zepto.js 官方网站推荐的方法。请参阅http://zeptojs.com/#download
This is the recommended method on zepto.js official site. See http://zeptojs.com/#download
虽然许多现有的答案在通过附加请求加载 Zepto.js 时工作得很好,但我有一种情况,我知道 Zepto 在大多数情况下就足够了,我只想将它与我的脚本合并,并在需要时延迟加载 jQuery。我为 Zepto 制作了一个小包装就能做到这一点。
它运行 “官方”
'__proto__' in ...
测试 并在失败时延迟加载 jQuery。如果成功,则会继续加载 Zepto。我发现即使加载了 Zepto,IE8 也会崩溃。这通过跳过模块的其余部分来解决这个问题。
对于乐观的情况,没有任何额外的脚本请求。对于 jQuery 路径,无论如何,这些用户并没有真正获得快速的体验。
While many of the existing answers work fine when loading Zepto.js via an additional request, I have a situation where I know Zepto will suffice most of the time and I just want to merge it in with my scripts and lazily load jQuery if needed. I put together a small wrapper for Zepto will do just that.
It runs the "offical"
'__proto__' in ...
test and lazy loads jQuery if it fails. If it succeeds, then it continues loading Zepto.I found that IE8 would blow up if Zepto was even loaded. This fixes that by skipping the rest of the module.
For the optimistic case, there isn't any additional script requests. For the jQuery path, well, those users weren't exactly getting the fast experience anyway.
这是一个老话题,但它是我想到的,而且我对整个解决方案并不满意。上面的评论中有人提到,官方的 zepto 测试将导致 zepto 使用 FireFix 3.6 而不是 JQuery,如果可能的话,我宁愿避免这种情况。
所以,我的想法是...测试一下它是否支持某些 HTML5 功能并且(如果它不是 IE)。这可能意味着更大的 jQuery 将适用于更多的浏览器,但我更喜欢“工作”臃肿的代码,而不是快速下载任何内容。所以,无论如何,采用 Modernizer 的 isCanvasSupported() 方法和 zepto 推荐的 __proto__ 测试,我认为这可能是一个很好的解决方案(还没有机会实际测试):
然后,只需在 document.write() 中使用该方法,如上面的示例所示,或者在定义 jquery/zepto 路径的任何地方。
我在快速交叉引用中看到的唯一两个支持 canvas 但 zepto 不支持的浏览器版本是:
* IOS Safari 3.2(Zepto 支持 4+)
* Android 2.1(Zepto 支持 2.2+)
http://zeptojs.com/#platforms
http://caniuse.com/#feat=canvas
This is an old topic, but it's what came up for me, and I was not happy with the solution overall. Someone in a comment above mentioned that the official zepto test will result in zepto going to FireFix 3.6 instead of JQuery, which I would prefer to avoid if at all possible.
So, my thought was...test to see if it supports some HTML5 feature AND if it's not IE. This may mean that the larger jQuery will go to more browsers than it should, but I would prefer "working" bloated code to a quick download of nothing. So, anyway, taking the isCanvasSupported() method from Modernizer and the
__proto__
test recommended by zepto, I'm thinking this might be a good solution (haven't had a chance to actually test yet):Then, just use that method in the document.write() as in the examples above or wherever you are defining the path to jquery/zepto.
The only two browser versions that I could see in a quick cross-reference that support canvas but aren't supported by zepto are:
* IOS Safari 3.2 (4+ is supported by Zepto)
* Android 2.1 (2.2+ is supported by Zepto)
http://zeptojs.com/#platforms
http://caniuse.com/#feat=canvas
我就是这样做的:
This is the way I do it:
您应该提高一点标准,这样不仅 IE8 能够获得 jQuery,其他旧版浏览器也能获得 jQuery。例如,Zepto 需要 Array.prototype.some 等功能。
Zepto 需要与 picoQuery (它是 Zepto 的替代品)大致相同的功能。在 picoQuery 中,它们确实是这样的:
从兼容性表中我们可以看出,任何支持 Array.isArray 的浏览器也支持 querySelectorAll()、addEventListener()、dispatchevent、Array.prototype.indexOf 和 Array.prototype.some - 所有这些都用于Zepto
picoQuery 在这里描述了这个选择:
http://picoquery.com/the_fallback
You should raise the bar a bit so not only IE8 will get jQuery, but also other older browsers. Zepto for example requires features such as Array.prototype.some.
Zepto requires much the same features as picoQuery (which is an alternative to Zepto). In picoQuery, they do like this:
From compatibility tables we have that any browser that supports Array.isArray also supports querySelectorAll(), addEventListener(), dispatchevent, Array.prototype.indexOf and Array.prototype.some - all which are used in Zepto
picoQuery describes this choice here:
http://picoquery.com/the_fallback