JS to determine the mobile side or PC side

Source: Internet
Author: User

JS to determine whether the client is a PC or handheld mobile device
Method One

function IsPC () {
var useragentinfo = navigator.useragent;
var Agents = new Array ("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;
}
[HTML] View plain copy
if (/(Iphone|ipad|ipod|ios)/i.test (navigator.useragent)) {
alert (navigator.useragent);
Window.location.href = "iphone.html";
} else if (/(Android)/i.test (navigator.useragent)) {
alert (navigator.useragent);
Window.location.href = "android.html";
} else {
Window.location.href = "pc.html";
};
Method Two

var browser={
Versions:function () {
var u = navigator.useragent, app = Navigator.appversion;
return {//Mobile terminal browser version information
Trident:u.indexof (' Trident ') >-1,//ie core
Presto:u.indexof (' presto ') >-1,//opera core
Webkit:u.indexof (' AppleWebKit ') >-1,//Apple, Google kernel
Gecko:u.indexof (' Gecko ') >-1 && u.indexof (' khtml ') = =-1,//Firefox kernel
Mobile:!! U.match (/applewebkit.*mobile.*/),//Whether it is a mobile terminal
Ios:!! U.match (/\ (i[^;] +;( U;)? Cpu.+mac OS x/),//ios terminal
Android:u.indexof (' Android ') >-1 | | U.indexof (' Linux ') >-1,//android terminal or UC Browser
Iphone:u.indexof (' iphone ') >-1,//whether for iphone or Qqhd browser
Ipad:u.indexof (' ipad ') >-1,//whether ipad
Webapp:u.indexof (' Safari ') = =-1,//whether the Web should be program, no head with bottom
Weixin:u.indexof (' Micromessenger ') >-1,//whether
Qq:u.match (/\sqq/i) = = "QQ"//whether QQ
};
}(),
Language: (Navigator.browserlanguage | | navigator.language). toLowerCase ()
}

if (browser.versions.mobile | | browser.versions.ios | | browser.versions.android | |
Browser.versions.iPhone | | BROWSER.VERSIONS.IPAD) {
window.location = "http://m.zhaizhainv.com";
}

Method Three: excerpted from IM-QQ

var os = function () {
var ua = navigator.useragent,
Iswindowsphone =/(?: Windows Phone)/.test (UA),
Issymbian =/(?: SymbianOS)/.test (UA) | | Iswindowsphone,
Isandroid =/(?: Android)/.test (UA),
Isfirefox =/(?: Firefox)/.test (UA),
Ischrome =/(?: chrome| Crios)/.test (UA),
Istablet =/(?: ipad| PlayBook)/.test (UA) | | (Isandroid &&!/(?: Mobile)/.test (UA)) | | (Isfirefox &&/(?: Tablet)/.test (UA)),
Isphone =/(?: IPhone)/.test (UA) &&!istablet,
IsPc =!isphone &&!isandroid &&!issymbian;
return {
Istablet:istablet,
Isphone:isphone,
Isandroid:isandroid,
Ispc:ispc
};
}();
How to use
if (os.isandroid | | os.isphone) {

Alert ("-----");

}

Method Four:

if (/applewebkit.*mobile/i.test (navigator.useragent) | | (/midp| symbianos| nokia| Samsung| Lg| nec| tcl| alcatel| bird| dbtel| dopod| philips| haier| lenovo| mot-| nokia| sonyericsson| sie-| amoi| Zte/.test (navigator.useragent))) {
if (Window.location.href.indexOf ("mobile") <0) {
try{
if (/android|webos|iphone|ipod| Blackberry/i.test (navigator.useragent)) {
window.location.href= "Mobile page";
}else if (/ipad/i.test (navigator.useragent)) {
window.location.href= "Flat page";
}else{
Window.location.href= "Other mobile pages"
}
}catch (e) {}
}
}
Method Five
<script language= "JavaScript" >
platforms, devices, and operating systems
var system ={
Win:false,
Mac:false,
Xll:false
};
Testing 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 it is mobile Access will automatically jump to the caibaojian.com page
if (system.win| | system.mac| | SYSTEM.XLL) {
}else{
Window.location.href= "http://caibaojian.com";
}
</script>
Method Six
<script language= "JavaScript" >
function Is_mobile () {
var regex_match =/(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|htc|dopod| blazer|netfront|helio|hosin|huawei|novarra| coolpad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson| Philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-| Longcos|pantech|gionee|^sie-|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operas*mobi|opera*mini|320x320| 240x320|176x220)/I;
var u = navigator.useragent;
if (null = = u) {
return true;
}
var result = regex_match.exec (u);

if (null = = result) {
return False
} else {
return True
}
}
if (Is_mobile ()) {
document.location.href= ' http://caibaojian.com '; Modify http://caibaojian.com to the destination page address you want to jump to
}
</script>
Method 700-degree version WebApp

<!---identify the phone or computer JS start--->
<script language= "JavaScript" >
(function () {
var res = getrequest ();
var par = res[' index '];
if (par!= ' Gfan ') {
var ua=navigator.useragent.tolowercase ();
var contains=function (A, b) {
if (A.indexof (b)!=-1) {return true;}
};
Change the following http://caibaojian.com to your WAP mobile page address like my http://caibaojian.com
var tomobilevertion = function () {
window.location.href = ' http://caibaojian.com/'
}

If (Contains (UA, "ipad") | | (Contains (UA, "rv:1.2.3.4")) | | (Contains (UA, "0.0.0.0")) | | (Contains (UA, "8.0.552.237"))) {return False}
if ((Contains (UA, "Android") && contains (UA, "mobile")) | | (Contains (UA, "Android") && contains (UA, "Mozilla")) | | (Contains (UA, "Android") && contains (UA, "opera"))
|| Contains (UA, "Ucweb7") | | Contains (UA, "iphone")) {tomobilevertion ();}
}
})();
function Getrequest () {
var url = location.search; Gets the URL in the "?" String after the character
var therequest = new Object ();
if (Url.indexof ("?")! =-1) {
var str = URL.SUBSTR (1);
STRs = Str.split ("&");
for (var i = 0; i < strs.length; i + +) {
Therequest[strs[i].split ("=") [0]]=unescape (Strs[i].split ("=") [1]);
}
}
return therequest;
}
</script>
<!---identify the JS end of your phone or computer--->

JS to determine the mobile side or PC side

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.