Js makes judgments on PCs and mobile phones

Source: Internet
Author: User

Js makes judgments on PCs and mobile phones

If you are not adaptive, and you cannot use the same html page for both the pc and mobile terminals, you must write two websites (the PC uses the www domain name and the mobile phone uses the wap second-level domain name ), one PC, one mobile phone, so how can we identify whether a user logs on to your website from a PC client or a mobile client (and can directly jump to a mobile phone webpage without loading a PC webpage when entering the www domain name on the mobile phone client), this requires js to judge the client system.

I have three pieces of code here, all of which can achieve the same effect. The advantages and disadvantages depend on your needs.

However, there are also two questions here. Please read the code first and I will answer it later.

1.

<Script type = "text/javascript">

Function IsPC (){
Var userAgentInfo = navigator. userAgent;
Var Agents = ["Android", "iPhone ",
"SymbianOS", "Windows Phone ",
"IPad", "iPod"];
Var flag = true;
For (var v = 0; v <Agents. length; v ++ ){
If (userAgentInfo. indexOf (Agents [v])> 0 ){
Flag = false;
Break;
}
}
Return flag;
} </Script>

2.

<Script type = "text/javascript">

Function browserRedirect (){
Var sUserAgent = navigator. userAgent. toLowerCase ();
Var bIsIpad = sUserAgent. match (/ipad/I) = "ipad ";
Var bIsIphoneOs = sUserAgent. match (/iphone OS/I) = "iphone OS ";
Var bIsMidp = sUserAgent. match (/midp/I) = "midp ";
Var bIsUc7 = sUserAgent. match (/rv: 1.2.3.4/I) = "rv: 1.2.3.4 ";
Var bIsUc = sUserAgent. match (/ucweb/I) = "ucweb ";
Var bIsAndroid = sUserAgent. match (/android/I) = "android ";
Var bIsCE = sUserAgent. match (/windows ce/I) = "windows ce ";
Var bIsWM = sUserAgent. match (/windows mobile/I) = "windows mobile ";
If (! (BIsIpad | bIsIphoneOs | bIsMidp | bIsUc7 | bIsUc | bIsAndroid | bIsCE | bIsWM )){
Window. location. href = B page;
}
}
BrowserRedirect (); </script>


3

<Script type = "text/javascript">
Var system = {
Win: false,
Mac: false,
Xll: false
};

// Detection platform
Var p = navigator. platform;
System. win = p. indexOf ("Win") = 0;
System. mac = p. indexOf ("Mac") = 0;
System. x11 = (p = "X11") | (p. indexOf ("Linux") = 0 );
// Jump statement
If (system. win | system. mac | system. xll ){
Window. location. href = "http://www.weishengyule.cn ";
} Else {
Window. location. href = "http://wap.weishengyule.cn ";
}
</Script>

Question: js scripts run only after the HTML page is loaded. Will the Domain Name of the PC-side webpage be entered on the mobile phone end? The webpage will be fully loaded before the webpage is redirected to the webpage on the mobile phone end?

Scenario 1: The webpage on the mobile phone is displayed only after it is fully loaded.

Cause: 1. You didn't put the code in the first line of the head. 2. you loaded the Code with an external file.

Scenario 2: You can directly access the webpage on your mobile phone

Method: directly use the <script type = "text/javascript"> Code </script> method to avoid problems.

Question 2: Who is superior and who is inferior

I personally like the third

Reason: using variables to name a function will theoretically load it directly to run [according to what my master said]. I personally feel that the effects of the three functions are no different.

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.