Mobile phone access to the PC website automatically jump to the mobile site code

Source: Internet
Author: User

4G ERA, mobile site has been very common, the general mobile site has a two-level domain name to access, such as m.16css.com if the phone directly access to www.16css.com is a PC site, on the phone to browse the computer version of the site experience is very bad.

If you can mobile phone access to PC-side computer website automatically jump to the mobile site is good:

The code is as follows:

Put the following code in the home area

123456789101112131415161718192021222324252627282930313233343536373839404142 <SCRIPT LANGUAGE="JavaScript"> functionmobile_device_detect(url) {        var thisOS=navigator.platform;        varos=newArray("iPhone","iPod","iPad","android","Nokia","SymbianOS","Symbian","Windows Phone","Phone","Linux armv71","MAUI","UNTRUSTED/1.0","Windows CE","BlackBerry","IEMobile"); for(vari=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信息做判断  varcheck = 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)   returntrue;  }  returnfalse; } }mobile_device_detect("http://m.16css.com");</SCRIPT>

The http://m.16css.com in the penultimate line is the mobile URL you want to jump to.

There is a relatively simple JS code, test can also

123456789101112131415 <script type="text/javascript">try{varurlhash = 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>

The third section of code:

12345678910111213 <script type="text/javascript">// JavaScript Documentfunctionurlredirect() {    varsUserAgent = navigator.userAgent.toLowerCase();     if((sUserAgent.match(/(ipod|iphone os|midp|ucweb|android|windows ce|windows mobile)/i))) {        // PC跳转移动端        varthisUrl = window.location.href;        window.location.href = thisUrl.substr(0,thisUrl.lastIndexOf(‘/‘)+1)+‘mobile/‘;            }}urlredirect();</script>

Http://www.16css.com/page_code/1157.html

Mobile phone access to the PC website automatically jumps to the mobile website Code (GO)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.