Javascript 使用javascript检测浏览器类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2490452/
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
Using javascript to detect browser type
提问by Duber
I'm trying to use this line to detect browser type: IE or Firefox.
我正在尝试使用这一行来检测浏览器类型:IE 或 Firefox。
alert(isBrowser("Microsoft"));
but I get absolutely nothing, the alert doesn't even pop up. Not sure what I'm doing wrong.
但我什么也没得到,警报甚至没有弹出。不知道我做错了什么。
What would be the best practice way to detect browser type?
检测浏览器类型的最佳实践方法是什么?
采纳答案by lajuette
I hope this helps:
我希望这有帮助:
http://www.quirksmode.org/js/detect.html
http://www.quirksmode.org/js/detect.html
(it's a long script, so i don't want to post it here)
(这是一个很长的脚本,所以我不想在这里发布)
回答by Darin Dimitrov
Try this:
尝试这个:
alert(navigator.appName);
回答by Jonas Elfstr?m
I think jQuery got it right when they support testing for featuresinstead of just browser.
我认为 jQuery 在支持功能测试而不仅仅是浏览器时做对了。
回答by Alexey Marzan
For MSIE detection you may use JavaScript:
对于 MSIE 检测,您可以使用 JavaScript:
// This function returns Internet Explorer's major version number,
// or 0 for others. It works by finding the "MSIE " string and
// extracting the version number following the space, up to the decimal
// point, ignoring the minor version number
<SCRIPT LANGUAGE="JavaSCRIPT">
function msieversion()
{
var ua = window.navigator.userAgent
var msie = ua.indexOf ( "MSIE " )
if ( msie > 0 ) // If Internet Explorer, return version number
return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
else // If another browser, return 0
return 0
}
</SCRIPT>
Below is an example of how to call it anywhere in your html:
以下是如何在 html 中的任何位置调用它的示例:
<SCRIPT LANGUAGE="javascript">
if ( msieversion() >= 0 )
document.write ( "This is Internet Explorer" );
else
document.write ( "This is another browser" );
</SCRIPT>
http://support.microsoft.com/kb/167820http://support.microsoft.com/kb/167820
http://support.microsoft.com/kb/167820 http://support.microsoft.com/kb/167820
回答by JeroenEijkhof
A very good article on this comes from Quirksmode: http://www.quirksmode.org/js/support.html
关于这方面的一篇非常好的文章来自 Quirksmode:http://www.quirksmode.org/js/support.html
The script supplied by 'lajuette' is good but it doesn't make you much smarter. The same author explain his thinking behind the script in the above link and basically what he says is:
'lajuette' 提供的脚本很好,但它并没有让你变得更聪明。同一作者在上面的链接中解释了他对脚本背后的想法,基本上他说的是:
- It is notabout browser detection
- It is about object detection
- This leads to the knowledge of which browser is used.
- 这与浏览器检测无关
- 这是关于物体检测
- 这导致知道使用哪个浏览器。
回答by Anil Namde
This is basic for browser type detection but from this littel code its difficult to understand what going wrong.... Can u add body of isBrowser() that will help.
这是浏览器类型检测的基础,但是从这个 littel 代码很难理解出了什么问题......你能添加 isBrowser() 的主体,这将有所帮助。
回答by eagle
function whereUWantToDetectBrowser(){
if (navigator.appName == "Microsoft Internet Explorer")
intExp();
else
other();
}
function intExp(){
//do what you want to do for specifically Internet Explorer
}
function other(){
//do what you want to do for other browsers
}
this code solves the problem. Instead of calling functions from whereUWantToDetectBrowser(), if you write your specific code there, this will cause an error. And code will not run. Because a browser detects the code which it has to run (specific to each browser). and if you are distinguishing the code means the code is not working in some browsers, so you want to write it specifically for those browsers.
So, other()will have not effect in IE, as intExp()has no effect in other browsers.
这段代码解决了这个问题。whereUWantToDetectBrowser()如果您在那里编写特定代码,而不是从 调用函数,则会导致错误。并且代码不会运行。因为浏览器会检测它必须运行的代码(特定于每个浏览器)。如果您区分代码意味着该代码在某些浏览器中不起作用,因此您希望专门为这些浏览器编写它。所以,other()在 IE 中不会有影响,因为intExp()在其他浏览器中没有影响。
回答by Nitesh
The best and shortest way to find the browser type for IE is.. U can do same for other browser types
找到 IE 浏览器类型的最佳和最短方法是.. 你可以为其他浏览器类型做同样的事情
if (navigator.appName == "Microsoft Internet Explorer"){
// Ur piece of validation
}

