Navigator對判斷移動終端
阿新 • • 發佈:2021-06-17
Navigator物件:
Navigator物件包含有關瀏覽器的資訊。沒有應用於它的公開標準,但所有的瀏覽器都支援該物件。
Navigator物件屬性:
appVersion 瀏覽器的平臺和版本資訊。
userAgent客戶機發送伺服器的user-agent頭部的值 。
方法一:
<scripttype=
"text/javascript"
>
var
browser={
versions:
function
(){
var
u=navigator.userAgent,app=navigator.appVersion;
return
{
//移動終端瀏覽器版本資訊
trident:u.indexOf(
'Trident'
)>-1,
//IE核心
presto:u.indexOf(
'Presto'
)>-1,
//opera核心
webKit:u.indexOf(
'AppleWebKit'
)>-1,
//蘋果、谷歌核心
gecko:u.indexOf(
'Gecko'
)>-1&&u.indexOf(
'KHTML'
)==-1,
//火狐核心
mobile:!!u.match(/AppleWebKit.*Mobile.*/),
//是否為移動終端
ios:!!u.match(/\(i[^;]+;(U;)?CPU.+MacOSX/),
//ios終端
android:u.indexOf(
'Android'
)>-1||u.indexOf(
'Linux'
)>-1,
//android終端或者uc瀏覽器
iPhone:u.indexOf(
'iPhone'
)>-1||u.indexOf(
'Mac'
)>-1,
//是否為iPhone或者QQHD瀏覽器
iPad:u.indexOf(
'iPad'
)>-1,
//是否iPad
webApp:u.indexOf(
'Safari'
)==-1
//是否web應該程式,沒有頭部與底部
};
}(),
language:(navigator.browserLanguage||navigator.language).toLowerCase()
}
if
(browser.versions.mobile==
true
||browser.versions.ios==
true
||browser.versions.android==
true
){
if
(confirm(
"檢測到您是移動終端!是否跳轉至手機版?"
))
{
window.location.href=
"http://3geas.huat.edu.cn"
;
}
}
</script>
方法二:
window.location.href = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) ? "https://www.baidu.com/" : "http://news.baidu.com/";
或
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent))
{window.location.href = "https://www.baidu.com/";
} else {
window.location.href = "http://news.baidu.com/";
}