最新代码:(测试可用) 
- <script type="text/javascript">
 
 -     var userAgent = navigator.userAgent,
 
 -     rMsie = /(msie\s|trident.*rv:)([\w.]+)/,
 
 -     rFirefox = /(firefox)\/([\w.]+)/,
 
 -     rOpera = /(opera).+version\/([\w.]+)/,
 
 -     rChrome = /(chrome)\/([\w.]+)/,
 
 -     rSafari = /version\/([\w.]+).*(safari)/;
 
 -     var browser;
 
 -     var version;
 
 -     var ua = userAgent.toLowerCase();
 
 -     function uaMatch(ua) {
 
 -         var match = rMsie.exec(ua);
 
 -         if (match != null) {
 
 -             return { browser : "IE", version : match[2] || "0" };
 
 -         }
 
 -         var match = rFirefox.exec(ua);
 
 -         if (match != null) {
 
 -             return { browser : match[1] || "", version : match[2] || "0" };
 
 -         }
 
 -         var match = rOpera.exec(ua);
 
 -         if (match != null) {
 
 -             return { browser : match[1] || "", version : match[2] || "0" };
 
 -         }
 
 -         var match = rChrome.exec(ua);
 
 -         if (match != null) {
 
 -             return { browser : match[1] || "", version : match[2] || "0" };
 
 -         }
 
 -         var match = rSafari.exec(ua);
 
 -         if (match != null) {
 
 -             return { browser : match[2] || "", version : match[1] || "0" };
 
 -         }
 
 -         if (match != null) {
 
 -             return { browser : "", version : "0" };
 
 -         }
 
 -     }
 
 -     var browserMatch = uaMatch(userAgent.toLowerCase());
 
 -     if (browserMatch.browser) {
 
 -         browser = browserMatch.browser;
 
 -         version = browserMatch.version;
 
 -     }
 
 -     alert(browser+" "+version);
 
 - </script>
 
  复制代码 
 
对象/特征检测法 该方法是一种判断浏览器能力(而非浏览器的确切型号)的通用方法。大部分JS专家认为这个方法最合适,因为他们认为按照该方法所编写的脚本是经得起未来考验的。 - //获取IE浏览器的版本号
 
 - //返回数值,显示IE的主版本号
 
 - function getIEVer() {
 
 -     var ua = navigator.userAgent; //获取用户端信息
 
 -     var b = ua.indexOf("MSIE "); //检测特殊字符串"MSIE "的位置
 
 -     if (b < 0) {
 
 -     return 0;
 
 -     }
 
 -     return parseFloat(ua.substring(b + 5, ua.indexOf(";", b))); //截取版本号字符串,并转换为数值
 
 - }
 
 - alert(getIEVer()); //返回数值8(我的IE8)
 
  复制代码如果更关注浏览器的能力而不在乎它实际的身份,就可以使用这种方法。 user-agent字符串检测法 user-agent字符串提供了关于Web浏览器的大量信息,包括浏览器的名称和版本。 - var ua = navigator.userAgent.toLowerCase(); //获取用户端信息
 
 - var info = {
 
 - ie: /msie/.test(ua) && !/opera/.test(ua), //匹配IE浏览器
 
 - op: /opera/.test(ua), //匹配Opera浏览器
 
 - sa: /version.*safari/.test(ua), //匹配Safari浏览器
 
 - ch: /chrome/.test(ua), //匹配Chrome浏览器
 
 - ff: /gecko/.test(ua) && !/webkit/.test(ua) //匹配Firefox浏览器
 
 - };
 
 - (info.ie) && alert("IE浏览器");
 
 - (info.op) && alert("Opera浏览器");
 
 - (info.sa) && alert("Safari浏览器");
 
 - (info.ff) && alert("Firefox浏览器");
 
 - (info.ch) && alert("Chrome浏览器");
 
  复制代码- function checkBrowser(type){
 
 -     var ua = navigator.userAgent.toLowerCase();     //获取用户端信息
 
 -     var info = {
 
 -       ie: /msie/.test(ua) && !/opera/.test(ua),     //匹配IE浏览器
 
 -       op: /opera/.test(ua),                         //匹配Opera浏览器
 
 -       sa: /version.*safari/.test(ua),               //匹配Safari浏览器
 
 -       ch: /chrome/.test(ua),                        //匹配Chrome浏览器
 
 -       ff: /gecko/.test(ua) && !/webkit/.test(ua)    //匹配Firefox浏览器
 
 -     };
 
 -     if(type=="ie"){
 
 -         return info.ie;
 
 -     }
 
 -     else if(type=="op"){
 
 -         return info.op;
 
 -     }
 
 -     else if(type=="sa"){
 
 -         return info.sa;
 
 -     }
 
 -     else if(type=="ch"){
 
 -         return info.ch;
 
 -     }
 
 -     else if(type=="ff"){
 
 -         return info.ff;
 
 -     }
 
 - }
 
 - if(checkBrowser("ie")){}    //调用
 
 - if(checkBrowser("ch")){}
 
  复制代码通常我们做得最多的,就是判断是否是IE了,其它几种浏览器一般都会符合标准.有些客户只需要符合IE和FF就已经满足了.那么我们可以这样做: var isIE = (navigator.appName == "Microsoft Internet Explorer"); 判断IE远远不止上面一种方法,可以使用IE更多特有的东西,如:window.ActiveXObject,document.all等,这些都属于对象/特征检测法了!通常要在不同的浏览器上写不同的样式(因为IE样式解析也各有不同),那就得判断版本了.可以这样做  
- //获取IE浏览器的版本号
 
 - //返回数值,显示IE的主版本号
 
 - function getIEVer() {
 
 -     var ua = navigator.userAgent; //获取用户端信息
 
 -     var b = ua.indexOf("MSIE "); //检测特殊字符串"MSIE "的位置
 
 -     if (b < 0) {
 
 -     return 0;
 
 -     }
 
 -     return parseFloat(ua.substring(b + 5, ua.indexOf(";", b))); //截取版本号字符串,并转换为数值
 
 - }
 
 - alert(getIEVer()); //返回数值7
 
  复制代码检测操作系统 - var isWin = (navigator.userAgent.indexOf("Win") != -1); //如果是Windows系统,则返回true
 
 - var isMac = (navigator.userAgent.indexOf("Mac") != -1); //如果是Macintosh系统,则返回true
 
 - var isUnix = (navigator.userAgent.indexOf("X11") != -1); //如果是Unix系统,则返回true
 
 - var isLinux = (navigator.userAgent.indexOf("Linux") != -1); //如果是Linux系统,则返回true
 
  复制代码JS代码判断IE6,IE7,IE8,IE9的函数代码 JS代码判断浏览器版本,支持IE6,IE7,IE8,IE9!做网页有时候会用到JS检测IE的版本,下面是检测Microsoft Internet Explorer版本的三种代码  
第一种: - <script type="text/javascript">
 
 - var browser=navigator.appName
 
 - var b_version=navigator.appVersion
 
 - var version=b_version.split(";");
 
 - var trim_Version=version[1].replace(/[ ]/g,"");
 
 - if(browser=="Microsoft Internet Explorer" && trim_Version=="MSIE6.0")
 
 - {
 
 - alert("IE 6.0");
 
 - }
 
 - else if(browser=="Microsoft Internet Explorer" && trim_Version=="MSIE7.0")
 
 - {
 
 - alert("IE 7.0"); window.location.href="http://xxxx.com";
 
 - }
 
 - else if(browser=="Microsoft Internet Explorer" && trim_Version=="MSIE8.0")
 
 - {
 
 - alert("IE 8.0");
 
 - }
 
 - else if(browser=="Microsoft Internet Explorer" && trim_Version=="MSIE9.0")
 
 - {
 
 - alert("IE 9.0");
 
 - }
 
 - </script>
 
  复制代码第二种: - <script type="text/javascript">
 
 - if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE6.0")
 
 - {
 
 - alert("IE 6.0");
 
 - }
 
 - else if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE7.0")
 
 - {
 
 - alert("IE 7.0");
 
 - }
 
 - else if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE8.0")
 
 - {
 
 - alert("IE 8.0");
 
 - }
 
 - else if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE9.0")
 
 - {
 
 - alert("IE 9.0");
 
 - }
 
 - </script>
 
  复制代码第三种: - <script type="text/javascript">
 
 - if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/7./i)=="7.")
 
 - {
 
 - alert("IE 7.0");
 
 - }
 
 - else if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/8./i)=="8.")
 
 - {
 
 - alert("IE 8.0");
 
 - }
 
 - else if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/9./i)=="9.")
 
 - {
 
 - alert("IE 9.0");
 
 - }
 
 - else if(navigator.appName == "Microsoft Internet Explorer")
 
 - {
 
 - alert("IE 6.0");
 
 - }
 
 - </script>
 
  复制代码 
  
 |