DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> javascript判斷移動端訪問設備並解析對應CSS的方法教程
javascript判斷移動端訪問設備並解析對應CSS的方法教程
編輯:關於JavaScript     

本文實例講述了javascript判斷移動端訪問設備並解析對應CSS的方法。分享給大家供大家參考。具體分析如下:

javascript判斷不同web訪問環境,主要針對移動設備,提供相對應的解析方案(判斷設備代碼直接copy騰訊網的):
代碼如下:// 判斷是否為移動端運行環境 
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>");}

加載頁面:
代碼如下:<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>

希望本文所述對大家的javascript程序設計有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved