JS how to determine whether the user is on the PC side and or mobile end access _javascript Tips

Source: Internet
Author: User
We have been working on our team's "Coffee Wings" recently, and in this project we have provided a great experience for the mobile platform. Along with Android platform's booming development. Not only is it driving the domestic smartphone industry, but many domestic developers are also starting to dive into the big wave of Android mobile terminals. If a lot of the Internet big wave you missed. So this Android wave you must not miss. At present, we have developed mobile terminals and Android clients for the "coffee Wings", using Android or iOS mobile phones, we can directly access the domain name www.sygxy.cn to watch the mobile terminal effect. At the same time, Android client has shelves to Tencent "Application Treasure" and "Baidu mobile phone assistant" and other applications mall. You can directly search the "wings of coffee" for downloading. In the absence of any promotion of the situation, only a week on the shelves, already has more than 100+ download volume.

If the mobile terminal needs to be optimized, what must be done is to determine what the client's access device is. By judging the result, return the unable domain name address, then load the different CSS files.

The way we use it is to judge by the User-agent value. Use the UserAgent property of the Navigator object in the JavaScript frame. The Navigator object has no public standards, but it is now supported by all browsers on the market. Use the UserAgent property to return the value of the User-agent header sent by the client server. Header information has a user-agent, which is to tell the server, user client what browser, as well as the operating system information. Use regular expressions to obtain user-agent values. and local values, this match can be a mobile operating system such as Android,ios, or a browser name. There is a match to jump, otherwise it will not jump, directly into the PC end of the home page.

Specific code implementation, we use the JavaScript scripting language to improve the portability of web sites. The benefits of using this technique seem to me to have two points

1: Can improve the portability of the site, if in the future of the Web project, our team needs to add a mobile terminal effect, it can easily introduce this part of the JS code, to judge. Improve the efficiency of the future.

2: The 2nd is to facilitate the Web program can be used in the interface loading, whether to introduce this part of the mobile Terminal judgment code.

Specific implementation:

We have to judge the access to the script extraction, if which page needs to be judged by the client terminal, we directly in the page using <script></script> to introduce a script, the specific judge code as follows:
Copy Code code as follows:

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| Windows Phone)/i)) {location.replace (f)}}}}catch (d) <br> {<br>}<br>}

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.