使用 javascript 确定正在使用的浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10505966/
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-10-26 10:06:42  来源:igfitidea点击:

Determine what Browser being used, using javascript

javascripthtml

提问by Christian Eric Paran

I have a problem in Determining Browsers.

我在确定浏览器时遇到问题。

I've tried using navigatorand well, it did not help.

我试过使用navigator,好吧,它没有帮助。

I used alert(navigator.appName);to determine the browser and I'm currently using Google Chrome, when the pop up appears it displayed Mozilla, but in Mozillait works fine and with MozillaIt self.

我曾经alert(navigator.appName);确定浏览器,我目前正在使用Google Chrome,当弹出窗口出现时,它显示出来Mozilla,但在Mozilla它里面工作正常,并且Mozilla它自己。

is there a problem with the code? or it's some bug?

代码有问题吗?或者这是一些错误?

采纳答案by JakeParis

To answer your question, no there is no problem or bug. Chrome represents itself as Mozilla. See this for the exact User Agent strings which Chrome gives.

要回答您的问题,没有问题或错误。Chrome 将自己表示为 Mozilla。有关 Chrome 提供的确切用户代理字符串,请参阅此内容。

http://www.useragentstring.com/pages/useragentstring.php?name=Chrome

http://www.useragentstring.com/pages/useragentstring.php?name=Chrome

Here are some examples:

这里有些例子:

Chrome 20.0.1092.0

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6

Chrome 20.0.1090.0

Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6

铬 20.0.1092.0

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6

铬 20.0.1090.0

Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6

回答by kennebec

navigator.sayswho= (function(){
    var N= navigator.appName, ua= navigator.userAgent, tem,
    M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*([\d\.]+)/i);
    if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
    M= M? [M[1], M[2]]:[N, navigator.appVersion, '-?'];
    return M.join(' ');
})();

alert(navigator.sayswho)

回答by Alexander Blacks

It's close to chrome, if you need a simple short solution try to use this:

它接近 chrome,如果您需要一个简单的简短解决方案,请尝试使用它:

function getBrowser() {
  if( navigator.userAgent.indexOf("Chrome") != -1 ) {
    return "Chrome";
  } else if( navigator.userAgent.indexOf("Opera") != -1 ) {
    return "Opera";
  } else if( navigator.userAgent.indexOf("MSIE") != -1 ) {
    return "IE";
  } else if( navigator.userAgent.indexOf("Firefox") != -1 ) {
    return "Firefox";
  } else {
    return "unknown";
  }
}

回答by Lyn Headley

Try navigator.appVersion, it should be more specific.

试试看navigator.appVersion,应该更具体。

回答by empire1138

Here's a link to really good js file that should answer all your questions:

这是一个非常好的 js 文件的链接,它应该可以回答您的所有问题:

http://www.quirksmode.org/js/detect.html

http://www.quirksmode.org/js/detect.html

回答by Bakudan

The browser sniffing wikipediaMDCis not considered a good practice. What if there is new browser, not publically available? The detection should be towards features not browsers. Browsers may change, became outdated, features are persistent.

浏览器嗅探维基百科MDC不被认为是一个好习惯。如果有新的浏览器,而不是公开可用的怎么办?检测应该针对功能而不是浏览器。浏览器可能会改变,变得过时,功能是持久的。

Just for the of completeness and the spirit of adventure - there is a way to test for specific JavaScript object:

只是为了完整性和冒险精神 - 有一种方法可以测试特定的 JavaScript 对象:

isChrome = function() { return !!(window.chrome);}
isOpera = function() { return !!(window.opera);}

For IE there is this magic thingy called conditional compilation SO Questionand materials about it MSDNJSkit.

对于 IE,有一个神奇的东西叫做条件编译SO Question和关于它的材料MSDN JSkit