A-A+

javascript中各种判断浏览器的方法

2016年10月13日 前端设计 暂无评论 阅读 6 views 次

以前有讲过很多关于判断浏览器各种js程序,下面我来总结一下比较实例的js检测浏览器的程序,有需要的朋友可参考参考。

利用small tricks or hacks来判断浏览器,代码如下:

  1. var ie = !+"v1",   
  2.     ie1 = !!top.execScript,   
  3.     ie2 = (function() {   
  4.         /*@cc_on  
  5.         @if (@_jscript)  
  6.             return true;  
  7.         @else */  
  8.             return false;   
  9.         /*  
  10.         @end  
  11.         @*/  
  12.     })(),   
  13.     ie3 = !('__proto__' in {}),   
  14.     ie4 = (function(IE){   
  15.         try{   
  16.             IE=this.window=!1;   
  17.         }catch(e){   
  18.             IE=!0;   
  19.         };   
  20.         return IE;   
  21.     })(),   
  22.     ie5 = /*@cc_on!@*/false,   
  23.     IEVersion = 0/*@cc_on+ScriptEngineMajorVersion()@*/';   
  24.     
  25. alert(IEVersion);  

判断是来为IE浏览器,代码如下:

  1. function IsIExplorer()   
  2. {   
  3.     var UA = navigator.userAgent;   
  4.     is360se = UA.toLowerCase().indexOf('360se') > -1 ? true : false;   
  5.     issougou = UA.toLowerCase().indexOf('metasr') > -1 ? true : false;   
  6.     //alert(UA.toLowerCase());   
  7.     if (!window.ActiveXObject || is360se || issougou) {   
  8.         alert('请您使用Microsoft Internet Explorer(IE)浏览器登录本系统!');   
  9.         window.opener = null;  //xiariboke.net  
  10.         window.open(' ', '_self', ' ');   
  11.         window.close();   
  12.     }   
  13. }  

实例三,代码如下:

  1. <title>JS检查是什么浏览器</title>  
  2. <script language="JavaScript" type="text/javascript">  
  3. function checkFirefoxOrIE(){  
  4. userAgent=window.navigator.userAgent.toLowerCase();  
  5. if(userAgent.indexOf("firefox")>=1){  
  6. Findex=userAgent.indexOf("firefox/");  
  7. versionName=userAgent.substr(Findex+"Firefox/".length,3);  
  8. document.write("你用的是火狐浏览器!版本是:Firefox/"+versionName+"<br>");  
  9. //xiariboke.net  
  10. else {  
  11.     var name=navigator.appName;  
  12.     if(name=="Microsoft Internet Explorer"){document.write("你用的是IE浏览器!");}  
  13.     }  
  14. }  
  15. </script>  
  16. </head>  
  17. <body onload="checkFirefoxOrIE();">  
  18. </body>  

上面的方法都是我们常用的并且能判断现在主流的一些浏览器类型了。

标签:

给我留言