A-A+
javascript中各种判断浏览器的方法
以前有讲过很多关于判断浏览器各种js程序,下面我来总结一下比较实例的js检测浏览器的程序,有需要的朋友可参考参考。
利用small tricks or hacks来判断浏览器,代码如下:
- var ie = !+"v1",
- ie1 = !!top.execScript,
- ie2 = (function() {
- /*@cc_on
- @if (@_jscript)
- return true;
- @else */
- return false;
- /*
- @end
- @*/
- })(),
- ie3 = !('__proto__' in {}),
- ie4 = (function(IE){
- try{
- IE=this.window=!1;
- }catch(e){
- IE=!0;
- };
- return IE;
- })(),
- ie5 = /*@cc_on!@*/false,
- IEVersion = 0/*@cc_on+ScriptEngineMajorVersion()@*/';
- alert(IEVersion);
判断是来为IE浏览器,代码如下:
- function IsIExplorer()
- {
- var UA = navigator.userAgent;
- is360se = UA.toLowerCase().indexOf('360se') > -1 ? true : false;
- issougou = UA.toLowerCase().indexOf('metasr') > -1 ? true : false;
- //alert(UA.toLowerCase());
- if (!window.ActiveXObject || is360se || issougou) {
- alert('请您使用Microsoft Internet Explorer(IE)浏览器登录本系统!');
- window.opener = null; //xiariboke.net
- window.open(' ', '_self', ' ');
- window.close();
- }
- }
实例三,代码如下:
- <title>JS检查是什么浏览器</title>
- <script language="JavaScript" type="text/javascript">
- function checkFirefoxOrIE(){
- userAgent=window.navigator.userAgent.toLowerCase();
- if(userAgent.indexOf("firefox")>=1){
- Findex=userAgent.indexOf("firefox/");
- versionName=userAgent.substr(Findex+"Firefox/".length,3);
- document.write("你用的是火狐浏览器!版本是:Firefox/"+versionName+"<br>");
- } //xiariboke.net
- else {
- var name=navigator.appName;
- if(name=="Microsoft Internet Explorer"){document.write("你用的是IE浏览器!");}
- }
- }
- </script>
- </head>
- <body onload="checkFirefoxOrIE();">
- </body>
上面的方法都是我们常用的并且能判断现在主流的一些浏览器类型了。