JS判斷移動端訪問設(shè)備并加載對應(yīng)CSS樣式
更新時間:2014年06月13日 10:15:18 投稿:whsnow
JS判斷不同web訪問環(huán)境,主要針對移動設(shè)備,提供相對應(yīng)的解析方案,本例是加載不同的css樣式
JS判斷不同web訪問環(huán)境,主要針對移動設(shè)備,提供相對應(yīng)的解析方案(判斷設(shè)備代碼直接copy騰訊網(wǎng)的)
// 判斷是否為移動端運行環(huán)境
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)){
// 判斷訪問環(huán)境是 Android|webOS|iPhone|iPod|BlackBerry 則加載以下樣式
setActiveStyleSheet("style_mobile_a.css");
}
else if(/iPad/i.test(navigator.userAgent)){
// 判斷訪問環(huán)境是 iPad 則加載以下樣式
setActiveStyleSheet("style_mobile_iPad.css");
}
else{
// 判斷訪問環(huán)境是 其他移動設(shè)備 則加載以下樣式
setActiveStyleSheet("style_mobile_other.css");
}
}
catch(e){}
}
}
else{
// 如果以上都不是,則加載以下樣式
setActiveStyleSheet("style_mobile_no.css");
}
// 判斷完畢后加載樣式
function setActiveStyleSheet(filename){document.write("<link href="+filename+" rel=stylesheet>");}
加載頁面
<script type="text/javascript">
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)){
window.location.href="手機頁面";
}else if(/iPad/i.test(navigator.userAgent)){
window.location.href="平板頁面";
}else{
window.location.href="其他移動端頁面"
}
}catch(e){}
}
}
</script>
復(fù)制代碼 代碼如下:
// 判斷是否為移動端運行環(huán)境
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)){
// 判斷訪問環(huán)境是 Android|webOS|iPhone|iPod|BlackBerry 則加載以下樣式
setActiveStyleSheet("style_mobile_a.css");
}
else if(/iPad/i.test(navigator.userAgent)){
// 判斷訪問環(huán)境是 iPad 則加載以下樣式
setActiveStyleSheet("style_mobile_iPad.css");
}
else{
// 判斷訪問環(huán)境是 其他移動設(shè)備 則加載以下樣式
setActiveStyleSheet("style_mobile_other.css");
}
}
catch(e){}
}
}
else{
// 如果以上都不是,則加載以下樣式
setActiveStyleSheet("style_mobile_no.css");
}
// 判斷完畢后加載樣式
function setActiveStyleSheet(filename){document.write("<link href="+filename+" rel=stylesheet>");}
加載頁面
復(fù)制代碼 代碼如下:
<script type="text/javascript">
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)){
window.location.href="手機頁面";
}else if(/iPad/i.test(navigator.userAgent)){
window.location.href="平板頁面";
}else{
window.location.href="其他移動端頁面"
}
}catch(e){}
}
}
</script>
相關(guān)文章
JS?中的類Public,Private?和?Protected詳解
這篇文章主要介紹了JS中的類Public,Private和Protected詳解,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08
IE與Firefox在JavaScript上的7個不同句法分享
盡管那需要用長串的、沉悶的不同分支代碼來應(yīng)付不同瀏覽器的日子已經(jīng)過去,偶爾還是有必要做一些簡單的區(qū)分和目標(biāo)檢測來確保某塊代碼能在用戶的機器上正常運行2011-10-10

