JS to judge the PC or the mobile side to open the Web page

Source: Internet
Author: User

Recently in the mobile site, also need to be compatible with the PC side. Have not found a better way, can only use JAVASCR to determine whether the user is open on the PC side or mobile.

JS Judgment

var IsPC = function () {
var useragentinfo = Navigator.userAgent.toLowerCase ();
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;
}

According to the judgment, introduce different CSS style files

var IsPC = IsPC ();
function Js_onload () {
var head=document.getelementsbytagname ("Head");
var es=document.createelement ("link");
if (!ISPC) {
es.href= ' Css/mobile.css ';
}else{
es.href= ' Css/pc.css '
}
Es.rel= "stylesheet";
Es.type= "Text/css";
Head[0].appendchild (es);

}
Js_onload ();

This is a little trouble!!! Perhaps the follow-up will find a better way to write these compatible

JS to judge the PC or the mobile side to open the Web page

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.