Javascript 检查浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10257810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript check browser
提问by Ravindra Gullapalli
does anyone have a script for checking for old browsers. It would have to follow this criteria:
有没有人有检查旧浏览器的脚本。它必须遵循以下标准:
Allow firefox 3.6 or up
Allow Google Chrome 15 and up
Allow Safari 5 or up
Block IE and opera
Block all other browsers
允许 Firefox 3.6 或更高版本
允许使用 Google Chrome 15 及更高版本
允许 Safari 5 或更高版本
阻止 IE 和歌剧
阻止所有其他浏览器
回答by Ravindra Gullapalli
You can use navigator
object for this and in that you can use userAgent
property like
您可以navigator
为此使用对象,并且您可以使用userAgent
类似的属性
if (navigator.userAgent.indexOf('Firefox') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Firefox') + 8)) >= 3.6){//Firefox
//Allow
}else if (navigator.userAgent.indexOf('Chrome') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Chrome') + 7).split(' ')[0]) >= 15){//Chrome
//Allow
}else if(navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Version') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Version') + 8).split(' ')[0]) >= 5){//Safari
//Allow
}else{
// Block
}
回答by Ayman Safadi
I agree with with @Sirko. Preferably, you shoulduse feature-detection instead. An alternative to to Modernizrwould be jQuery's $.support()
function. It's not exactlythe same thing, but it may suit your needs.
我同意@Sirko。最好,您应该改用特征检测。Modernizr的替代品是 jQuery 的$.support()
功能。这并不完全相同,但它可能适合您的需求。
If you insist on browser-detection, you can write your own script using jQuery's $.browser()
function.
如果你坚持浏览器检测,你可以使用jQuery的$.browser()
功能编写自己的脚本。