首页 > 代码库 > javascript宿主对象之window.navigator

javascript宿主对象之window.navigator

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();

 

javascript宿主对象之window.navigator