【问题标题】:Distinguish Web Browsers From Each Other With Javascript Document Element使用 Javascript 文档元素区分 Web 浏览器
【发布时间】:2014-08-12 15:16:08
【问题描述】:
function init()
      {
        // Microsoft Internet Explorer
   alert("test-1");
        if (document.all)
        {
   alert("test-2");
          document.all.loading.style.visibility="hidden";
          document.all.myapplet.style.visibility="visible";
          document.applets[0].repaint();
        }
        // Netscape Navigator

        else
        {
alert("test-3");
          document.loading.visibility="hide";
          document.myapplet.visibility="visible";
        }
      }

这个 javascript 代码在小程序完全加载后清除加载文本。第一个条件是针对微软操作系统的浏览器。第二个条件适用于 unix 和其他浏览器。但是所有的浏览器都会给出 test1 和 test-3 的警告信息。即并非所有浏览器都知道“document.all”。如何使用 javascript(尤其是 microsoft internet explorer)区分浏览器?

【问题讨论】:

  • 你试过谷歌吗?搜索“使用javascript识别浏览器”的答案给出了许多可能的解决方案

标签: javascript progress-bar webbrowser-control loading japplet


【解决方案1】:

var isOpera = !!window.opera || navigator.userAgent.indexOf('OPR/') >= 0;
var isFirefox = typeof InstallTrigger !== 'undefined';
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor')>0;
var isChrome = !!window.chrome && !isOpera;
var isIE = 假 ||文档.文档模式;

也许这段代码是为了检测浏览器而运行的..

【讨论】:

    【解决方案2】:

    使用jQuery

    代码sn-p:

      <script src="/jquery.js" type="text/javascript">
      </script> 
      <script src="/jquery.browser.js" type="text/javascript">
      </script> 
      <script language="JavaScript" type="text/javascript">
        document.writeln( "<pre>" );
        document.writeln( "User Agent        = " + navigator.userAgent );
        document.write  ( "Browser Vendor    = " );
        if( $.browser.chrome )
        {
          document.writeln( "Google Inc" );
        }
        if( $.browser.mozilla )
          {
          document.writeln( "Mozilla Foundation" );
          }
        if( $.browser.msie )
          {
          document.writeln( "Microsoft Corporation" );
          }
        if( $.browser.opera )
          {
          document.writeln( "Opera Software" );
          }
        if( $.browser.safari )
          {
          document.writeln( "Apple Inc" );
          }
        document.write  ( "Browser           = " );
        if( $.browser.chrome )
          {
          document.write( "Chrome" );
          }
        if( $.browser.mozilla )
          {
          document.write( "Mozilla" );
          }
        if( $.browser.msie )
          {
          document.write( "Internet Explorer" );
          }
        if( $.browser.opera )
          {
          document.write( "Opera" );
          }
        if( $.browser.safari )
          {
          document.write( "Safari" );
          }
        if( $.browser.desktop )
          {
          document.writeln( " (Desktop)" );
          }
        if( $.browser.mobile )
          {
          document.writeln( " (Mobile)" );
          }
        document.writeln( "Version Number    = " + $.browser.versionNumber );
        document.writeln( "Rendering Version = " + $.browser.version );
        document.write  ( "Operating System  = " );
        if( $.browser.android )
          {
          document.writeln( "Android" );
          }
        if( $.browser.cros )
          {
          document.writeln( "Chrome OS" );
          }
        if( $.browser.linux )
          {
          document.writels( "Linux" );
          }
        if( $.browser.mac )
          {
          document.writels( "Macintosh" );
          }
        if( $.browser.win )
          {
          document.writeln( "Windows" );
          }
        document.writeln( "</pre>" );
      </script>
    

    输出:

    User Agent        = Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0
    Browser Vendor    = Mozilla Foundation
    Browser           = Mozilla (Desktop)
    Version Number    = 31
    Rendering Version = 31.0
    Operating System  = Windows
    

    【讨论】:

      猜你喜欢
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 2022-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多