Javascript code used to determine whether the client is a mobile phone or a PC _ javascript skills

Source: Internet
Author: User
Javascript is used to determine whether the client is a PC or a handheld device. Sometimes it is required in a project for convenient detection. There are two methods for source generation:

The Code is as follows:

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;
}


2. Type 2:

The Code is as follows:

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 ();



Baidu's judgment code

The Code is 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/" +;
B = true
} Else {
If (isSubdomain (arguments [1], e) = 2 ){
F = f + "/# m/" +;
B = true
} Else {
F =;
B = false
}
}
} Else {
B = true
}
If (B ){
Var c = window. location. hash;
If (! C. match ("fromapp ")){
If (navigator. userAgent. match (/(iPhone | iPod | Android | ios | SymbianOS)/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 ()){
Return 2
} Else {
Return 0
}
}
}
};


Usage:

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.