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

Source: Internet
Author: User

Reprinted from: http://www.jb51.net/article/48939.htm

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:

functionIsPC () {varUseragentinfo =navigator.useragent; varAgents = ["Android", "IPhone",                "SymbianOS", "Windows Phone",                "IPad", "IPod"]; varFlag =true;  for(varv = 0; v < agents.length; v++) {        if(Useragentinfo.indexof (agents[v]) > 0) {flag=false;  Break; }    }    returnFlag;}

2. The second type:

functionBrowserredirect () {varSuseragent =navigator.userAgent.toLowerCase (); varBisipad = Suseragent.match (/ipad/i) = = "ipad"; varBisiphoneos = Suseragent.match (/iphone os/i) = = "iphone OS"; varBISMIDP = Suseragent.match (/midp/i) = = "MIDP"; varBIsUc7 = Suseragent.match (/rv:1.2.3.4/i) = = "rv:1.2.3.4"; varBISUC = Suseragent.match (/ucweb/i) = = "UCWeb"; varBisandroid = Suseragent.match (/android/i) = = "Android"; varBisce = Suseragent.match (/windows ce/i) = = "Windows CE"; varBISWM = 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

functionUaredirect (f) {Try {  if(document.getElementById ("Bdmark")! =NULL) {   return  }  varb =false; if(arguments[1]) {   varE =Window.location.host; varA =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) {varc =Window.location.hash; if(!c.match ("Fromapp")) {    if((Navigator.userAgent.match (/(iphone|ipod| android|ios| SymbianOS)/i)) {Location.replace (f)} }}}Catch(d) {}}functionIssubdomain (c, D) { This. GetDomain =function(f) {varE = F.indexof ("://"); if(E > 0) {   varH = f.substr (e + 3)  } Else {   varh =F}varg =/^www\./; if(G.test (h)) {h= H.substr (4)  }  returnh}; if(c = =d) {return1 } Else {  varc = This. GetDomain (c); varb = This. GetDomain (d); if(c = =b) {return1  } Else{C= C.replace (".", "\ \.")); varA =NewREGEXP ("\ \" + C + "$")); if(B.match (a)) {return2   } Else {    return0   }  } }};

How to use:
<script Type=text/javascript>uaredirect ("Mobile Station", "Web site");</script> another article, feel inferior to the above good, but we can refer to the next

varBrowser_class =navigator.useragent;varbrowser_class_name1 = Browser_class.match ("Mobile");varBrowser_class_name2 = Browser_class.match ("mobile");varLocation_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 (GO)

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.