How to use JS to determine whether the current page is a PC or mobile-opened

Source: Internet
Author: User

1. Using Regular Expressions and three-mesh operators , meaning that if the mobile side is open, then jump to "https:www.baidu.com/", if not just jump to "http://new.baidu.com/"

Window.location.href =/android|webos|iphone|ipod| Blackberry/i.test (navigator.useragent)? "https://www.baidu.com/":  "http://news.baidu.com/";

Equivalent to

if (/android|webos|iphone|ipod| Blackberry/i.test (navigator.useragent)) {    window.location.href = "https://www.baidu.com/";} else {    Window.location.href = "http://news.baidu.com/";}

2. The second method

function IsPC () {    var useragentinfo = navigator.useragent;    var Agents = ["Android", "IPhone",                "SymbianOS", "Windows Phone",                "IPad", "IPod");    var flag = true;     < Agents . length; v++) {        >  0) {            flag = false;            break;        }    }    return flag;}

3. The third method

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

4. Baidu's method of judging

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

The above is Baidu summary, convenient oneself just!

How to use JS to determine whether the current page is a PC or mobile-opened

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.