JavaScript judges Mobile and PC-side access to different websites

Source: Internet
Author: User

http://www.html-js.com/article/2677

Now many sites are divided into two versions, a PC end of a mobile (except the response), for these two versions, you need to access the device to judge, if it is a PC, direct access to the PC website, or access to the mobile site.

For this problem can be resolved by judging UA, the front-end JS can judge, the back-end judgment is OK, here we mainly discuss how to deal with JS.

If we have a website, the PC is accessed via www.test.com, and the mobile is accessed through m.test.com. What we need to do is to jump directly to m.test.com when the www.test.com is accessed by the mobile side. At this point we just have to deal with it, add the following JS code to the head of the page:

(function(){var URL= Location. href;Replace www.test.com with your domainIf((URL.IndexOf(' Www.test.com ')!= -1 ) && navigator.useragentmatch (/(iphone|ipod| Android|ios|ipad)/i) {location.href = Span class= "token string" > ' http://m.test.com ' ; }})      

However, in most cases it is not just that simple to jump directly from www.test.com to m.test.com. Our site in addition to the hostname part, followed by the following, such as: www.test.com/list/98/, for such a url,pc directly such access, for mobile, need to through m.test.com/list/98/can show a better effect.

Then, you can use the regular to handle, when the mobile access, we replace "http://www" with "http://m" (Colon is the English colon), and then update the page to see the page on the mobile side of the effect of rendering. The specific code is as follows:

(function(){var URL= Location. href;Replace www.test.com with your domainIf((URL.IndexOf(' Www.test.com ')!=-1)&& Navigator. useragent.Match(/(iphone|ipod| Android|ios|ipad)/i) {var newurl = Urlreplace ( ' http://www ') Span class= "token punctuation",  ' http://m ' ) ; Location.href = Newurl }})      

Ok, the above is the mobile and PC-side website access issues.

JavaScript judges Mobile and PC-side access to different websites

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.