【问题标题】:Using javascript to detect browser type [duplicate]使用javascript检测浏览器类型[重复]
【发布时间】:2011-01-30 05:35:13
【问题描述】:

我正在尝试使用这一行来检测浏览器类型:IE 或 Firefox。

alert(isBrowser("Microsoft"));

但我一无所获,甚至没有弹出警报。不知道我做错了什么。

检测浏览器类型的最佳实践方法是什么?

【问题讨论】:

    标签: javascript internet-explorer firefox


    【解决方案1】:

    我希望这会有所帮助:

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

    (脚本很长,这里就不贴了)

    【讨论】:

      【解决方案2】:

      试试这个:

      alert(navigator.appName);
      

      【讨论】:

      • 我喜欢这个答案的简单性。有没有人反对它,因为尽管其他答案更全面(封面版本号等),但我需要的只是浏览器类型。但是这里有任何已知的问题。对了,我在FF上试了一下,说的是网景!
      • 我在 Chrome 上试过了,它也说 netscape :s
      • Chrome、Firefox 和 Safari 返回名称“Netscape”。
      • @user3716264 和 IE11,检查一下here.
      • Brave and Edge 也返回 'netscape'...
      【解决方案3】:

      我认为当 jQuery 支持 testing for features 而不仅仅是浏览器时,它是正确的。

      【讨论】:

      • 我同意这个理论——而且大多数情况下它在实践中也有效。但有时您需要了解浏览器。例如,您如何检测 webkit(safari 或 chrome)中的鼠标右键单击,甚至prototype.js 也必须在此处求助于浏览器检测
      【解决方案4】:

      对于 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>
      

      以下是如何在 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/167820 http://support.microsoft.com/kb/167820

      【讨论】:

      • 如果您能在此处使用代码给出真正的答案并提供链接作为您从中获取该信息的备份,那就更好了。
      • 我已经更正了我的答案。
      • "msieversion() >= 0" 应为 "msieversion > 0"。否则所有浏览器都会返回“这是 Internet Explorer”。
      • chrome 返回“这是 Internet Explorer”
      【解决方案5】:

      Quirksmode 的一篇非常好的文章来自于:http://www.quirksmode.org/js/support.html

      'lajuette' 提供的脚本很好,但不会让你变得更聪明。同一作者在上面的链接中解释了他对脚本背后的想法,基本上他说的是:

      • 不是关于浏览器检测的
      • 关于物体检测
      • 这会导致了解所使用的浏览器。

      【讨论】:

      • 无论javascript框架如何,这个答案都会对您有所帮助,我认为这是最好的起点。
      【解决方案6】:

      This is basic for browser type detection 但是从这个小代码中很难理解出了什么问题......你可以添加 isBrowser() 的正文来帮助吗?

      【讨论】:

        【解决方案7】:
        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
        }
        

        这段代码解决了这个问题。如果您在此处编写特定代码,而不是从whereUWantToDetectBrowser() 调用函数,这将导致错误。并且代码不会运行。因为浏览器检测到它必须运行的代码(特定于每个浏览器)。如果您要区分代码,则意味着该代码在某些浏览器中不起作用,因此您想专门为这些浏览器编写它。 所以other()在IE下是无效的,因为intExp()在其他浏览器下是无效的。

        【讨论】:

          【解决方案8】:

          查找 IE 浏览器类型的最佳和最短的方法是.. 其他浏览器类型也可以这样做

          if (navigator.appName == "Microsoft Internet Explorer"){
          
          // Ur piece of validation 
          
          }
          

          【讨论】:

          • 它仅适用于 IE 10 及更早版本。检查它here.
          猜你喜欢
          • 2012-11-27
          • 2018-03-24
          • 2014-02-21
          • 2012-09-20
          • 2011-09-26
          • 2011-09-26
          • 1970-01-01
          • 2013-12-04
          • 2013-12-15
          相关资源
          最近更新 更多