window.navigator.userAgent.toLowerCase()
javascript
userAgent 屬性是一個只讀的字符串,聲明瞭瀏覽器用於 HTTP 請求的用戶代理頭的值。html
例如:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)java
function isWechat(){
let agent = window.navigator.userAgent.toLowerCase()
if (/micromessenger/.test(agent)) {
return true
}
}
複製代碼
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
//判斷iPhone|iPad|iPod|iOS
window.location.href ="iPhone.html";
} else if (/(Android)/i.test(navigator.userAgent)) {
//判斷Android
window.location.href ="Android.html";
} else {
//pc
window.location.href ="pc.html";
};
複製代碼
function userBrowser () {
var browserName = navigator.userAgent.toLowerCase();
if(/msie/i.test(browserName) && !/opera/.test(browserName)){
alert("IE");
return ;
} else if (/firefox/i.test(browserName)){
alert("Firefox");
return ;
} else if (/chrome/i.test(browserName) &&/webkit/i.test(browserName) && /mozilla/i.test(browserName)){
alert("Chrome");
return ;
}else if(/opera/i.test(browserName)){
alert("Opera");
return ;
} else if(/webkit/i.test(browserName) &&!(/chrome/i.test(browserName) && /webkit/i.test(browserName) && /mozilla/i.test(browserName))){
alert("Safari");
return ;
} else{
alert("unKnow");
}
}
複製代碼