JS判斷不同web存取環境,主要針對行動設備,提供相對應的解析方案(判斷設備程式碼直接copy騰訊網的)
複製程式碼
程式碼如下:
// 判斷是否為行動端運作環境
if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (userAgent) || /MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent) )){
if(window.location.href.indexOf("?mobile")
try{
if(/Android|webOS|iPhone |iPod|BlackBerry/i.test(navigator.userAgent)){
// 判斷存取環境為Android|webOS|iPhone|iPod|BlackBerry 則載入以下樣式
setActiveStyleSheet("style_mobile_"style_mobile_astyle_mobile_astyle_mobile_astyle_mobile_astyle_mobile_astyle_mobile .css");
}
else if(/iPad/i.test(navigator.userAgent)){
// 判斷存取環境是iPad 則載入以下樣式
setActiveStyleSheet("style_mobile_iPad.css");
}
else{
// 存取環境載入環境是其他行動裝置則載入以下樣式
setActiveStyleSheet("style_mobile_other.css");
}
}
catch(e){}
}
}
else{
// 如果以上都不是,則載入以下樣式
setActiveStyleSheet("style_mobile_no.css");
}
// 判斷完成後載入樣式
function setActiveStyleSheet(filename){document.write("<link href=" filename " rel=stylesheet>");}
複製程式碼
程式碼如下:
程式碼如下:
}catch(e){} } }