JS判斷移動端訪問設備並解析對應CSS

JS判斷不同web訪問環境,主要針對移動設備,提供相對應的解析方案(判斷設備代碼直接copy騰訊網的)

// 判斷是否爲移動端運行環境
// wukong.name 20130716
if(/AppleWebKit.*Mobile/i.test(navigator.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")<0){
try{
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
// 判斷訪問環境是 Android|webOS|iPhone|iPod|BlackBerry 則加載以下樣式
setActiveStyleSheet("style_mobile_a.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>");}

原文地址:http://www.w3ctech.com/p/1524

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章