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: