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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:31:35  来源:igfitidea点击:

Javascript check browser

javascriptbrowsercross-browser

提问by Ravindra Gullapalli

does anyone have a script for checking for old browsers. It would have to follow this criteria:

有没有人有检查旧浏览器的脚本。它必须遵循以下标准:

  1. Allow firefox 3.6 or up

  2. Allow Google Chrome 15 and up

  3. Allow Safari 5 or up

  4. Block IE and opera

  5. Block all other browsers

  1. 允许 Firefox 3.6 或更高版本

  2. 允许使用 Google Chrome 15 及更高版本

  3. 允许 Safari 5 或更高版本

  4. 阻止 IE 和歌剧

  5. 阻止所有其他浏览器

回答by Ravindra Gullapalli

You can use navigatorobject for this and in that you can use userAgentproperty 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()功能编写自己的脚本。