js 如何判断是否是谷歌浏览器

JavaScript08

js 如何判断是否是谷歌浏览器,第1张

JavaScript判断浏览器类型一般有两种办法,一种是根据各种浏览器独有的属性来分辨,另一种是通过分析浏览器的userAgent属性来判断的。在许多情况下,值判断出浏览器类型之后,还需判断浏览器版本才能处理兼容性问题,而判断浏览器的版本一般只能通过分析浏览器的userAgent才能知道。 判断浏览器的代码如下:<script type="text/javascript">var Sys = {}var ua = navigator.userAgent.toLowerCase()if (window.ActiveXObject) Sys.ie = ua.match(/msie ([\d.]+)/)[1] else if (document.getBoxObjectFor) Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1] else if (window.MessageEvent &&!document.getBoxObjectFor) Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1] else if (window.opera) Sys.opera = ua.match(/opera.([\d.]+)/)[1] else if (window.openDatabase) Sys.safari = ua.match(/version\/([\d.]+)/)[1]//以下进行测试 if(Sys.ie) document.write('IE: '+Sys.ie)if(Sys.firefox) document.write('Firefox: '+Sys.firefox)if(Sys.chrome) document.write('Chrome: '+Sys.chrome)if(Sys.opera) document.write('Opera: '+Sys.opera)if(Sys.safari) document.write('Safari: '+Sys.safari)</script>下面介绍各种浏览器的userAgent:1、IEMozilla/4.0 (compatibleMSIE 8.0Windows NT 6.0) Mozilla/4.0 (compatibleMSIE 7.0Windows NT 5.2) Mozilla/4.0 (compatibleMSIE 6.0Windows NT 5.1) Mozilla/4.0 (compatibleMSIE 5.0Windows NT) 版本号是MSIE之后的数字2、Firefox Mozilla/5.0 (WindowsUWindows NT 5.2) Gecko/2008070208 Firefox/3.0.1 Mozilla/5.0 (WindowsUWindows NT 5.1) Gecko/20070309 Firefox/2.0.0.3 Mozilla/5.0 (WindowsUWindows NT 5.1) Gecko/20070803 Firefox/1.5.0.12 版本号是Firefox之后的数字3、Opera Opera/9.27 (Windows NT 5.2Uzh-cn) Opera/8.0 (MacintoshPPC Mac OS XUen) Mozilla/5.0 (MacintoshPPC Mac OS XUen) Opera 8.0 版本号是靠近Opera的数字4、Safari Mozilla/5.0 (WindowsUWindows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13 Mozilla/5.0 (iPhoneUCPU like Mac OS X) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3 版本号是Version之后的数字5、Chrome Mozilla/5.0 (WindowsUWindows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13 版本号在Chrome只后的数字

主要用到navigator.userAgent

代码如下:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

</head>

<body>

<script type="text/javascript">

function myBrowser(){

var userAgent = navigator.userAgent//取得浏览器的userAgent字符串

console.log(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("compatible") >-1 &&userAgent.indexOf("MSIE") >-1 &&!isOpera) {

return "IE"

}//判断是否IE浏览器

}

var cc = myBrowser()

if(cc == "Chrome"){

alert("当前是Chrome浏览器")

}

if(cc == "IE"){

alert("当前是IE浏览器")

}

</script>

</body>

</html>

var browser = getBrowserInfo() //浏览器信息

//alert(browser)//IE 11.0

//IE11以下: MSIE 10.0、MSIE9.0等

//chrome:chrome/41.0.2272.89 [返回的是个数组]

//firefox: firefox/42.0 [返回的是个数组]

var verinfo = (browser+"").replace(/[^0-9.]/ig,"")//浏览器版本

//alert(verinfo)//11.0

//IE浏览器: 11.0/10.0/9.0

//chrome浏览器:41.0.2272.89

//Firefox浏览器: 42.0

function getBrowserInfo()

{

var agent = navigator.userAgent.toLowerCase()

//console.log(agent)

//agent chrome : mozilla/5.0 (windows nt 6.1wow64) applewebkit/537.36 (khtml, like gecko) chrome/41.0.2272.89 safari/537.36

//agent firefox : mozilla/5.0 (windows nt 6.1wow64rv:42.0) gecko/20100101 firefox/42.0

//agent IE11: mozilla/5.0 (windows nt 6.1wow64trident/7.0slcc2.net clr 2.0.50727.net clr 3.5.30729

//接上.net clr 3.0.30729media center pc 6.0infopath.2.net4.0c.net4.0erv:11.0) like gecko

(可以看出IE11中不包括MSIE字段)

//agent IE10: mozilla/5.0(compatiblemsie 10.0windows nt 6.1wow64trident/6.0)

var regStr_ie = /msie [\d.]+/gi

var regStr_ff = /firefox\/[\d.]+/gi

var regStr_chrome = /chrome\/[\d.]+/gi

var regStr_saf = /safari\/[\d.]+/gi

//IE11以下

if(agent.indexOf("msie") >0)

{

return agent.match(regStr_ie)

}

//IE11版本中不包括MSIE字段

if(agent.indexOf("trident") >0&&agent.indexOf("rv") >0){

return "IE " + agent.match(/rv:(\d+\.\d+)/) [1]

}

//firefox

if(agent.indexOf("firefox") >0)

{

return agent.match(regStr_ff)

}

//Chrome

if(agent.indexOf("chrome") >0)

{

return agent.match(regStr_chrome)

}

//Safari

if(agent.indexOf("safari") >0 &&agent.indexOf("chrome") <0)

{

return agent.match(regStr_saf)

}

}