navigator是Brower对象(浏览器对象),查看navigator的属性和方法
怎样用navigator来识别浏览器呢?
根据navigator.userAgent返回值识别
function validBrowser(){
var u_agent = navigator.userAgent;
var browser_name='Failed to identify the browser';
if(u_agent.indexOf('Firefox')>-1){
browser_name='Firefox';
}else if(u_agent.indexOf('Chrome')>-1){
browser_name='Chrome';
}else if(u_agent.indexOf('Trident')>-1&&u_agent.indexOf('rv:11')>-1){
browser_name='IE11';
}else if(u_agent.indexOf('MSIE')>-1&&u_agent.indexOf('Trident')>-1){
browser_name='IE(8-10)';
}else if(u_agent.indexOf('MSIE')>-1){
browser_name='IE(6-7)';
}else if(u_agent.indexOf('Opera')>-1){
browser_name='Opera';
}else{
browser_name+=',info:'+u_agent;
}
document.write('browser_name:'+browser_name+'<br>');
document.write('u_agent:'+u_agent+'<br>');
}
附几种浏览的user_agent
这是我使用navigator.userAgent获取的360浏览器极速模式下的数据:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
此数据为什么找不到360字眼的,因为360浏览器极速模式用的是chrom的内核,360的兼容模式用的是IE内核
关于用navigator属性来判断用户是PC端访问还是移动端访问:
浏览器内核判断:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<base >
<title></title>
<script>
function bro(){
var is360 = false;
var isIE = false;
var isFirefox = false;
var isCrome = false;
var broName = '';
if(window.navigator.userAgent.indexOf('MSIE')!=-1&&window.navigator.appName.indexOf("Microsoft") != -1){
isIE = true;
broName = 'IE';
}
if(window.navigator.userAgent.indexOf('Firefox')!=-1){
isFirefox = true;
broName = 'Firefox';
}
if(window.navigator.userAgent.indexOf('Chrome')!=-1){
if(window.navigator.webkitPersistentStorage){
isCrome = true;
broName = 'Chrome';
}else{
is360 = true;
broName = '360';
}
}
document.getElementById('broType').value=broName;
}
</script>
</head>
<body onload="bro();">
<input type="text" id="broType" name="broType">
</body>
</html>