js判断各个浏览器的方法

来源:互联网 发布:python在spss中的应用 编辑:程序博客网 时间:2024/06/01 10:09

方法一:        

function getExplorer() {            var explorer = window.navigator.userAgent;            //ie             if (explorer.indexOf("MSIE") >= 0) {                alert("ie");            }            //firefox             else if (explorer.indexOf("Firefox") >= 0) {                alert("Firefox");            }            //Chrome            else if (explorer.indexOf("Chrome") >= 0) {                alert("Chrome");            }            //Opera            else if (explorer.indexOf("Opera") >= 0) {                alert("Opera");            }            //Safari            else if (explorer.indexOf("Safari") >= 0) {                alert("Safari");            }        }



方法二:

<script>   if(!+[1,])alert("这是ie浏览器");      else alert("这不是ie浏览器");   </script>   


方法三:

        <script type="text/javascript">                var Sys = {};                var ua = navigator.userAgent.toLowerCase();                var s;                (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :                (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :                (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :                (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :                (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;                //以下进行测试                if (Sys.ie) document.write('IE: ' + Sys.ie);                if (Sys.firefox) document.write('Firefox: ' + Sys.firefox);                if (Sys.chrome) document.write('Chrome: ' + Sys.chrome);                if (Sys.opera) document.write('Opera: ' + Sys.opera);                if (Sys.safari) document.write('Safari: ' + Sys.safari);            </script>


0 0