JS to determine whether the client is a mobile phone or PC 2 code

Source: Internet
Author: User

Javascript to determine whether the client is a PC or handheld device, sometimes the project needs to use, very convenient detection, the source of the Oh, a total of two kinds of methods

1. The first kind:

Copy CodeThe 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. The second type:

Copy CodeThe 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

Copy CodeThe 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/" + 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| SymbianOS))) {
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 (a)) {
Return 2
} else {
return 0
}
}
}
};


How to use:
<script Type=text/javascript>uaredirect ("Mobile Station", "Web Station");</script>

Another article, feel as good as above, but we can refer to the next

Copy CodeThe code is as follows:
var browser_class = navigator.useragent;
var browser_class_name1 = Browser_class.match ("Mobile");
var browser_class_name2 = Browser_class.match ("mobile");
var location_url = window.location.href;
if (browser_class_name1! = NULL | | Browser_class_name2! = NULL) {
if (Location_url.match ("wap") = = null) {
Window.location.href = "http://wap.xxxx.com";
}
} else {
if (Location_url.match ("3g")! = NULL | | Location_url.match ("WAP")! = null) {
Window.location.href = "http://wap.xxxx.com";
}
}

JS to determine whether the client is a mobile phone or PC 2 code

Related Article

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.