手機訪問PC網站自動跳轉到手機網站代碼

方法一:

<SCRIPT LANGUAGE="JavaScript">

function mobile_device_detect(url){ 
    var thisOS=navigator.platform; 
    var os=new Array("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","Windows Phone","Phone","Linux armv71","MAUI","UNTRUSTED/1.0","Windows CE","BlackBerry","IEMobile");
 for(var i=0;i<os.length;i++) { 
 if(thisOS.match(os[i])){  
 window.location=url;
 } 
 }
  
 //因爲相當部分的手機系統不知道信息,這裏是做臨時性特殊辨認
 if(navigator.platform.indexOf('iPad') != -1){
 window.location=url;
 }
  
 //做這一部分是因爲Android手機的內核也是Linux
 //但是navigator.platform顯示信息不盡相同情況繁多,因此從瀏覽器下手,即用navigator.appVersion信息做判斷
 var check = navigator.appVersion;
  
 if( check.match(/linux/i) ) {
  //X11是UC瀏覽器的平臺 ,如果有其他特殊瀏覽器也可以附加上條件
  if(check.match(/mobile/i) || check.match(/X11/i)) {
  window.location=url;
  } 
 }
  
 //類in_array函數
 Array.prototype.in_array = function(e) {
 for(i=0;i<this.length;i++) {
  if(this[i] == e)
  return true;
 }
 return false;
 }

mobile_device_detect("http://***.***.com");

</SCRIPT>


方法二:

需要手機端也網頁的數據同步

<script type="text/javascript">
 (function(){var ua=navigator.userAgent.toLowerCase();var bIsIpad=ua.match(/ipad/i)=="ipad";var bIsIphoneOs=ua.match(/iphone os/i)=="iphone os";var bIsAndroid=ua.match(/android/i)=="android";var bIsWM=ua.match(/windows mobile/i)=="windows mobile";if(bIsIpad||bIsIphoneOs||bIsAndroid||bIsWM){window.location.href="http://m.jb51.net/android/game/826.html"}})();
 </script>


方法三:

<script type="text/javascript">
try {
var urlhash = window.location.hash;
if (!urlhash.match("fromapp")){
if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i))){
window.location="http://m.16css.com/";
}
}
}
catch(err){
}
</script>


方法四:
<script type="text/javascript">
// JavaScript Document
function urlredirect() {
    var sUserAgent = navigator.userAgent.toLowerCase(); 
    if ((sUserAgent.match(/(ipod|iphone os|midp|ucweb|android|windows ce|windows mobile)/i))) {
        // PC跳轉移動端
        var thisUrl = window.location.href;
        window.location.href = thisUrl.substr(0,thisUrl.lastIndexOf('/')+1)+'mobile/';  
    }
}
urlredirect();
</script>


方法五:

直接把以下代碼另存一個shouji.js寫到PC端網址模板首頁
<script src="/js/shouji.js" type="text/javascript"></script>
//這樣直接在手機上訪問PC端網址,即可在手機上實現自動跳轉對應的手機端網站
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
var mobileAgent = new Array("iphone", "ipod", "ipad", "android", "mobile", "blackberry", "webos", "incognito", "webmate", "bada", "nokia", "lg", "ucweb", "skyfire");
var browser = navigator.userAgent.toLowerCase();
var isMobile = false;
for (var i=0; i<mobileAgent.length; i++){ 
if (browser.indexOf(mobileAgent[i])!=-1){
isMobile = true;
location.href = 'http://m.nk91.com/';
break; 

}

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