JS to realize PC and mobile phone judgment

Source: Internet
Author: User

If you are not very adaptive, you can not use the PC and mobile Web pages are the same HTML page, then you have to write two stations (PC with WWW domain name, wap two domain name for mobile phone), a PC, a mobile phone, Then how to do to identify the user is the PC customer service or mobile phone client landing on your site (and can be implemented in the mobile phone input www domain name when not loading PC-side Web page directly to the mobile phone page), which requires JS to the client's system to judge

I have three pieces of code here, you can achieve this effect of the same, the pros and cons, depends on your needs

However, there are 2 questions, please look at the code, I will answer later

1.

<script type= "Text/javascript" >

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

2.

<script type= "Text/javascript" >

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


3

<script type= "Text/javascript" >
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 (system.win| | system.mac| | SYSTEM.XLL) {
Window.location.href= "http://www.weishengyule.cn";
}else{
Window.location.href= "http://wap.weishengyule.cn";
}
</script>

Question: JS script is in the HTML page after the loading of the run, I do not appear in the mobile phone input PC-side domain name will be fully loaded before the page to jump to the mobile phone end of the page

Scenario One: A full load will appear before jumping to the mobile page

Reason: 1. You didn't put the code on the first line of Head 2. You loaded it with an external file.

Scenario Two: Will not appear above the situation, directly into the mobile Web page

Method: Directly with <script type= "Text/javascript" > Code </script> this way, there will be no problem

Question two: Who is superior and who is inferior

Individuals like a third

Reason: To use a variable to name the function, in theory will directly load him to run "according to, my teacher said," the personal feeling three effects are no different

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.