js獲取瀏覽器對象的信息

js中有一個對象叫 navigator,navigator 對象包含有關瀏覽器的信息。全部的瀏覽器都支持該對象。
其中常常用到的是 navigator.userAgent 屬性,一般,它是在 navigator.appCodeName(瀏覽器的名稱) 的值以後加上斜線和 navigator.appVersion 的值構成的。
 
判斷是PC仍是手機瀏覽器:
第一種:
function IsPC() {
  var userAgentInfo = navigator.userAgent;
  var Agents = ["Android", "iPhone","SymbianOS", "Windows Phone", "iPad", "iPod"];
  var flag = true;
  for (var v = 0; v < Agents.length; v++) {
    if (userAgentInfo.indexOf(Agents[v]) > 0) {
      flag = false;
      break;
    }
  }
  return flag;
}

 

第二種:
function browserRedirect() {
  var sUserAgent = navigator.userAgent.toLowerCase();
  var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
  var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
  var bIsMidp = sUserAgent.match(/midp/i) == "midp";
  var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
  var bIsAndroid = sUserAgent.match(/android/i) == "android";
  var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
  var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
  if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
    window.location.href=B頁面;
  }
}
 
 
判斷是安卓仍是iOS
 
經常使用:
function ismobile(test){
  var u = navigator.userAgent, app = navigator.appVersion;
  if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
    if(window.location.href.indexOf("?mobile")<0){
      try{
        if(/iPhone|mac|iPod|iPad/i.test(navigator.userAgent)){
          return '0';
        }else{
          return '1';
        }
      }catch(e){}
      }
    }else if( u.indexOf('iPad') > -1){
      return '0';
    }else{
      return '1';
    }
};
相關文章
相關標籤/搜索