目前不少網站都是採用了響應式自適應頁面的設計了,根據訪問設備的不一樣,顯示不一樣的內容。可是仍是會有一些節奏比較慢的網站,仍是PC頁面和手機PAD頁面不一樣的訪問域名。正好我這裏有個須要,同一個域名要根據不一樣的訪問設備顯示PC頁面或者手機頁面,這裏收集兩個比較簡潔的方法,都是經過JS代碼實現的。javascript
第一個:css
<script type="text/javascript"> var userAgent = navigator.userAgent.toLowerCase(); var platform; if(userAgent == null || userAgent == ''){ platform = 'WEB' ; }else{ if(userAgent.indexOf("android") != -1 ){ platform = 'ANDROID'; location.href = "http://m.kuegou.com/"; }else if(userAgent.indexOf("ios") != -1 || userAgent.indexOf("iphone") != -1 || userAgent.indexOf("ipad") != -1){ platform = 'IOS'; location.href = "http://m.kuegou.com/"; }else if(userAgent.indexOf("windows phone") != -1 ){ platform = 'WP'; location.href = "http://m.kuegou.com/"; }else{ platform = 'WEB' ; location.href = "http://www.kuegou.com/"; } } </script>
第二個:java
這一個是兩段代碼,分別放到PC頁面網頁和手機頁面網頁,實現不一樣設備訪問不一樣頁面都能實現調整,好比電腦訪問了手機頁面的地址也會跳轉到PC頁面上來。android
首先是放入PC頁面的代碼:ios
<script type="text/javascript"> var url = window.location.pathname; var wapurl="http://3g.xxx.com"+url 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=wapurl; }else{ window.location.href=wapurl; } }catch(e){} } } </script>
下邊是放入手機頁面的代碼:web
<script type="text/javascript"> var url = window.location.pathname; var pcurl="http://www.xxx.com"+url if(/AppleWebKit.*Mobile/i.test(navigator.userAgent)==false || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))==false){ if(window.location.href.indexOf("?mobile")<0){ try{ if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)==false){ window.location.href=pcurl; } }catch(e){} } } </script>