国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

網(wǎng)站pc端自動(dòng)跳轉(zhuǎn)到手機(jī)端幾種代碼分享

2024-04-25 20:47:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
                現(xiàn)在的網(wǎng)站,一般都包含PC和手機(jī)版兩個(gè)頁(yè)面。那么,如何實(shí)現(xiàn)手機(jī)訪問(wèn)網(wǎng)站自動(dòng)跳轉(zhuǎn)到手機(jī)版呢?下面,vevb就總結(jié)幾個(gè)常用的js代碼,分享給大家。

1、最新版織夢(mèng)跳轉(zhuǎn)到手機(jī)版代碼(親測(cè)有效):

  1. <meta http-equiv="mobile-agent" content="format=xhtml;url={dede:global.cfg_mobileurl/}/index.php"
  2. <script type="text/javascript">if(window.location.toString().indexOf('pref=padindex') != -1){}else{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|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){window.location.href="{dede:global.cfg_mobileurl/}/index.php";}else if(/iPad/i.test(navigator.userAgent)){}else{}}catch(e){}}}}</script> 


注意:其它非織夢(mèng)網(wǎng)站只需將代碼中“{dede:global.cfg_mobileurl/}/index.php”改為您的網(wǎng)址即可,如:http://m.vevb.com/ 的形式。vevb.com親測(cè)有效!


2、

  1. <script type="text/javascript"
  2. try { 
  3. var urlhash = window.location.hash; 
  4. if (!urlhash.match("fromapp")) 
  5. if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i))) 
  6. window.location="http://m.your-website.com/"; //這里的網(wǎng)址請(qǐng)改為你手機(jī)站的網(wǎng)址 
  7. catch(err) 
  8. </script> 


3、

  1. <script type="text/javascript"
  2. // JavaScript Document 
  3. function urlredirect() { 
  4.     var sUserAgent = navigator.userAgent.toLowerCase();  
  5.     if ((sUserAgent.match(/(ipod|iphone os|midp|ucweb|android|windows ce|windows mobile)/i))) { 
  6.         // PC跳轉(zhuǎn)移動(dòng)端 
  7.         var thisUrl = window.location.href; 
  8.         window.location.href = thisUrl.substr(0,thisUrl.lastIndexOf('/')+1)+'mobile/'
  9.           
  10.     } 
  11. urlredirect(); 
  12. </script> 


4、利用百度Site App代碼判斷手機(jī)訪問(wèn):

  1. <script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script> 
  2. <script type="text/javascript">uaredirect("m.your-website.com");</script>? 

注::直接用上面的方法,貌似網(wǎng)頁(yè)會(huì)有一點(diǎn)問(wèn)題,我建議把代碼調(diào)整一下,直接把百度的那個(gè)JS文件里的代碼拿出來(lái)。整理后的代碼如下:

  1. <script language="JavaScript">function uaredirect(f){try{if(document.getElementById("bdmark")!=null){return}var b=false;if(arguments[1]){var e=window.location.host;var a=window.location.href;if(isSubdomain(arguments[1],e)==1){f=f+"/#m/"+a;b=true}else{if(isSubdomain(arguments[1],e)==2){f=f+"/#m/"+a;b=true}else{f=a;b=false}}}else{b=true}if(b){var c=window.location.hash;if(!c.match("fromapp")){if((navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i))){location.replace(f)}}}}catch(d){}}function isSubdomain(c,d){this.getdomain=function(f){var e=f.indexOf("://");if(e>0){var h=f.substr(e+3)}else{var h=f}var g=/^www/./;if(g.test(h)){h=h.substr(4)}return h};if(c==d){return 1}else{var c=this.getdomain(c);var b=this.getdomain(d);if(c==b){return 1}else{c=c.replace(".","//.");var a=new RegExp("//."+c+"$");if(b.match(a)){return 2}else{return 0}}}};uaredirect("m.your-website.com");</script> 

5、
  1. <SCRIPT LANGUAGE="JavaScript"
  2.  function mobile_device_detect(url) 
  3.  { 
  4.         var thisOS=navigator.platform; 
  5.         var os=new Array("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","Windows Phone","Phone","Linux armv71","MAUI","UNTRUSTED/1.0","Windows CE","BlackBerry","IEMobile"); 
  6.  for(var i=0;i<os.length;i++) 
  7.         { 
  8.  if(thisOS.match(os[i])) 
  9.         {   
  10.   window.location=url; 
  11.  } 
  12.            
  13.  } 
  14.  //因?yàn)橄喈?dāng)部分的手機(jī)系統(tǒng)不知道信息,這里是做臨時(shí)性特殊辨認(rèn) 
  15.  if(navigator.platform.indexOf('iPad') != -1) 
  16.         { 
  17.   window.location=url; 
  18.  } 
  19.  //做這一部分是因?yàn)锳ndroid手機(jī)的內(nèi)核也是Linux 
  20.  //但是navigator.platform顯示信息不盡相同情況繁多,因此從瀏覽器下手,即用navigator.appVersion信息做判斷 
  21.   var check = navigator.appVersion; 
  22.   if( check.match(/linux/i) ) 
  23.           { 
  24.    //X11是UC瀏覽器的平臺(tái) ,如果有其他特殊瀏覽器也可以附加上條件 
  25.    if(check.match(/mobile/i) || check.match(/X11/i)) 
  26.                  { 
  27.    window.location=url; 
  28.    }  
  29.  } 
  30.  //類in_array函數(shù) 
  31.  Array.prototype.in_array = function(e) 
  32.  { 
  33.   for(i=0;i<this.length;i++) 
  34.   { 
  35.    if(this[i] == e) 
  36.    return true
  37.   } 
  38.   return false
  39.  } 
  40.  } 
  41. mobile_device_detect("http://m.your-website.com"); 
  42. </SCRIPT> 


以上則是VeVb為大家整理的幾種實(shí)現(xiàn)網(wǎng)站pc端自動(dòng)跳轉(zhuǎn)到手機(jī)端幾種代碼分享。希望對(duì)您有所幫助!




注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到建站經(jīng)驗(yàn)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 金门县| 昌平区| 淮阳县| 镇原县| 将乐县| 栾城县| 合肥市| 连江县| 辉南县| 丁青县| 阜南县| 托克逊县| 巴里| 贵溪市| 绿春县| 南丰县| 永州市| 和龙市| 耒阳市| 高陵县| 拉孜县| 阳信县| 利津县| 广昌县| 宜兰市| 奉节县| 沂源县| 谢通门县| 获嘉县| 务川| 固始县| 互助| 新郑市| 屏山县| 垦利县| 烟台市| 孙吴县| 武宣县| 汝阳县| 芜湖县| 林芝县|