Loading

区分浏览器,判断浏览器版本

1.用JS判断浏览器是否是IE9以下,处理可能遇到的兼容性问题,或者给出浏览器版本过低的升级提醒。

  if(navigator.appName == "Microsoft Internet Explorer"&&parseInt(navigator.appVersion.split(";")[1].replace(/[ ]/g, "").replace("MSIE",""))<9){

    alert("您的浏览器版本过低,请下载IE9及以上版本");
  }
2.用JS判断浏览器是否是IE9及以下,处理可能遇到的兼容性问题,或者给出浏览器版本过低的升级提醒。

  if(navigator.appName == "Microsoft Internet Explorer" && (parseInt(navigator.appVersion.split(";")[1].replace(/[ ]/g, "").replace("MSIE",""))<9) || parseInt(navigator.appVersion.split(";")[1].replace(/[ ]/g, "").replace("MSIE",""))==9)){

    alert("您的浏览器版本过低,请下载IE9以上版本");
  }
3.判断不同浏览器,弹框显示。
  //定义函数  
  function myBrowser(){
      var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
      var isOpera = userAgent.indexOf("Opera") > -1;
      if (isOpera) {
          return "Opera"
      }; //判断是否Opera浏览器
      if (userAgent.indexOf("Firefox") > -1) {
          return "FF";
      } //判断是否Firefox浏览器
      if (userAgent.indexOf("Chrome") > -1){
      return "Chrome";
   }
      if (userAgent.indexOf("Safari") > -1) {
          return "Safari";
      } //判断是否Safari浏览器
     if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
          return "IE";
      }; //判断是否IE浏览器
  }

 

  //以下是调用上面的函数
  var mb = myBrowser();
  if ("IE" == mb) {
      alert("我是 IE");
  }
  if ("FF" == mb) {
      alert("我是 Firefox");
  }
  if ("Chrome" == mb) {
      alert("我是 Chrome");
  }
  if ("Opera" == mb) {
      alert("我是 Opera");
  }
  if ("Safari" == mb) {
      alert("我是 Safari");
  }

3.判断浏览器及IE低版本。
  function myBrowser(){
      var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
      var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
      var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
      var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器
      var isSafari = userAgent.indexOf("Safari") > -1; //判断是否Safari浏览器
      if (isIE) {
          var IE5 = IE55 = IE6 = IE7 = IE8 = false;
          var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
          reIE.test(userAgent);
          var fIEVersion = parseFloat(RegExp["$1"]);
          IE55 = fIEVersion == 5.5;
          IE6 = fIEVersion == 6.0;
          IE7 = fIEVersion == 7.0;
          IE8 = fIEVersion == 8.0;
          if (IE55) {
              return "IE55";
          }
          if (IE6) {
              return "IE6";
          }
          if (IE7) {
              return "IE7";
          }
          if (IE8) {
              return "IE8";
          }
      }//isIE end
      if (isFF) {
          return "FF";
      }
      if (isOpera) {
          return "Opera";
      }
  }//myBrowser() end
  //以下是调用上面的函数
  if (myBrowser() == "FF") {
      alert("我是 Firefox");
  }
  if (myBrowser() == "Opera") {
      alert("我是 Opera");
  }
  if (myBrowser() == "Safari") {
      alert("我是 Safari");
  }
  if (myBrowser() == "IE55") {
      alert("我是 IE5.5");
  }
  if (myBrowser() == "IE6") {
      alert("我是 IE6");
  }
  if (myBrowser() == "IE7") {
      alert("我是 IE7");
  }
  if (myBrowser() == "IE8") {
      alert("我是 IE8");
  }
4.判断IE浏览器7,8,9三个版本。
if(navigator.userAgent.indexOf("MSIE")>0){
if(navigator.userAgent.indexOf("MSIE 6.0")>0){
alert("ie6");
}
if(navigator.userAgent.indexOf("MSIE 7.0")>0){
alert("ie7");
}
//if(navigator.userAgent.indexOf("MSIE 9.0")>0 && !window.innerWidth){
  if(navigator.userAgent.indexOf("MSIE 8.0")>0){
        alert("ie8");
}
if(navigator.userAgent.indexOf("MSIE 9.0")>0){
alert("ie9");
}
}


输出navigator.userAgent测试
//ie9 : Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) 
//ie8 : Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
//ie7 : Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; Tablet PC 2.0; .NET4.0E; .NET4.0C)
//Firefox: Mozilla/5.0 (Windows NT 6.1; rv:20.0) Gecko/20100101 Firefox/20.0
//Chrome: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
//Opera: Opera/9.80 (Windows NT 6.1; Edition IBIS) Presto/2.12.388 Version/12.14
//Safari: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2
未完待续。。。
posted @ 2017-11-01 12:49  CassielPang  阅读(811)  评论(0编辑  收藏  举报