javascript宿主對象之window.navigator詳解,javascriptnavigator

來源:互聯網
上載者:User

javascript宿主對象之window.navigator詳解,javascriptnavigator

window.navigator用來反映瀏覽器及其功能資訊的對象。 

// 檢測瀏覽器版本資訊function getBrowserInfo(){  var Sys = {};  var ua = window.navigator.userAgent.toLowerCase();  var re =/(msie|firefox|chrome|opera|version).*?([\d.]+)/;  var m = ua.match(re);  Sys.browser = m[1].replace(/version/, "'safari");  Sys.ver = m[2];  return Sys;}var BomInfo = getBrowserInfo;console.log(BomInfo ());

如何判斷是不是ie呢?

var navigatorName = "Microsoft Internet Explorer"; var isIE = false;  if( window.navigator.appName == navigatorName ){    isIE = true;    alert("ie")  }else{    alert("not ie")  }  

還有一種方法:

if(window.addEventListener){    alert("not ie");  }else if(window.attachEvent){     alert("is ie");  }else{      alert("這種情況發生在不支援DHTML的老版本瀏覽器(現在一般都支援)")  }  

判斷裝置類型: 

function browerType() {   var sUserAgent = navigator.userAgent.toLowerCase(); //瀏覽器的使用者代理程式設定為小寫,再進行匹配   var isIpad = sUserAgent.match(/ipad/i) == "ipad"; //或者利用indexOf方法來匹配   var isIphoneOs = sUserAgent.match(/iphone os/i) == "iphone";   var isMidp = sUserAgent.match(/midp/i) == "midp"; //移動資訊裝置描述MIDP是一套Java應用編程介面,多適用於塞班系統   var isUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4"; //CVS標籤   var isUc = sUserAgent.match(/ucweb/i) == "ucweb";   var isAndroid = sUserAgent.match(/android/i) == "android";   var isCe = sUserAgent.match(/windows ce/i) == "windows ce";   var isWM = sUserAgent.match(/windows mobil/i) == "windows mobil";   if (isIpad || isIphoneOs || isMidp || isUc7 || isUc || isAndroid || isCe || isWM) {     alert('該裝置為行動裝置'); // 做某些事情   } else {     alert('該裝置為PC裝置'); // 做某些事情   }}browerType();

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.