2010-02-16 108 views
2

有沒有一種方式可以讓它成爲如此當用電話或其他小型移動設備查看網站時,該網站會自動識別出它正在被瀏覽,並重新定向到一個手機友好的子域鏡像還是自動調用使手機友好的腳本?製作一個移動友好網站

任何文章或建議將不勝感激!

回答

3

HTTP請求中的user-agent可以幫助檢測瀏覽器。然後,您可以在從特定瀏覽器查看頁面時重定向到子域。

否則,還可以使用media CSS選擇器來使佈局適應各種設備。有媒體類型handheld,但我不知道它支持多好。

+0

*拍打前額*偉大的想法!我將不得不研究一些流行的移動瀏覽器。 – Davey

2

使用多個CSS。

<link href="/css/mobile.css" rel="stylesheet" type="text/css" media="handheld" /> 
+1

這裏假設該網站是移動就緒,除了CSS。如果有一堆Flash,巨大的圖像等,這不像他們重定向到的專用移動網站那樣好。 – ceejayoz

0

那麼,當然你可以使用JavaScript代碼來檢查用戶運行什麼樣的瀏覽器,並對此作出適當的迴應。例如:

var browser = navigator.appName; 

if (browser == 'Mozilla Firefox') { 
// do something 
} else if (browser == 'Some Weird Browser') { 
// do something else 
} 

參考:http://www.w3schools.com/js/js_browser.asp

+0

假設手機支持JavaScript,那就是...... – ceejayoz

0

有幾種方法可以幫助你檢測的移動瀏覽器。下面是一些示例PHP代碼:

function isMobileBrowser() { 

    if(isset($_SERVER["HTTP_X_WAP_PROFILE"])) return true; 

    if(preg_match("/wap\.|\.wap/i",$_SERVER["HTTP_ACCEPT"])) return true; 

    if(isset($_SERVER["HTTP_USER_AGENT"])){ 

     // Quick Array to kill out matches in the user agent 
     // that might cause false positives 

     $badmatches = array("OfficeLiveConnector","MSIE\ 8\.0","OptimizedIE8","MSN\ Optimized","Creative\ AutoUpdate","Swapper"); 

     foreach($badmatches as $badstring){ 
     if(preg_match("/".$badstring."/i",$_SERVER["HTTP_USER_AGENT"])) return false; 
     } 

     // Now we'll go for positive matches 

     $uamatches = array("midp", "j2me", "avantg", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda",  "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto","webos"); 

     foreach($uamatches as $uastring){ 
     if(preg_match("/".$uastring."/i",$_SERVER["HTTP_USER_AGENT"])) return true; 
     } 

    } 
    return false; 
} 

來源:http://www.brainhandles.com/techno-thoughts/detecting-mobile-browsers