JavaScript determines whether the source of the access is a cell phone or a computer, and what kind of browser _javascript tips

Source: Internet
Author: User

JS determine what type of browser

Copy Code code as follows:

if (Window.sidebar && "object" = = typeof (Window.sidebar) && "function" = = typeof (Window.sidebar.addPan EL))//Firefox

{
}
else if (document.all && "object" = = typeof (window.external))//IE

{
}


JS is used to distinguish between IE and other browsers and Ie6-8 methods.

1, document.all
2 、!! Window. ActiveXObject;

Use the following methods:

if (document.all) {
Alert ("IE browser");
}else{
Alert ("Non IE browser");
}

if (!! Window. ActiveXObject) {
Alert ("IE browser");
}else{
Alert ("Non IE browser");
}

Here are the ways to differentiate between IE6, IE7, and IE8:

var isie=!! Window. ActiveXObject;
var Isie6=isie&&!window. XMLHttpRequest;
var isie8=isie&&!! Document.documentmode;
var isie7=isie&&!isie6&&!isie8;
if (Isie) {
if (isIE6) {
Alert ("Ie6″");
}else if (isIE8) {
Alert ("Ie8″");
}else if (isIE7) {
Alert ("Ie7″");
}
}

First of all we make sure that this browser is in the case of IE, conducted at a time of detection, if you have doubts about this, you can test.

I use this directly in judgment, and you can also declare them as variables to use first. It is said that Firefox will also join document.all this way, so it is recommended that the second method should be safer.

Using Navigator.userAgent.indexOf () to differentiate multiple browsers, the code example is as follows:

Copy Code code as follows:

<coding-1 lang= "Other" >
<script type= "Text/javascript" >
var browser={
Versions:function () {
var u = navigator.useragent, app = Navigator.appversion;
return {
Trident:u.indexof (' Trident ') >-1,//ie kernel
Presto:u.indexof (' presto ') >-1,//opera kernel
Webkit:u.indexof (' AppleWebKit ') >-1,//Apple, Google kernel
Gecko:u.indexof (' Gecko ') >-1 && u.indexof (' khtml ') = = 1,//Firefox kernel
Mobile:!! U.match (/applewebkit.*mobile.*/) | |!! U.match (/applewebkit/),//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 | | U.indexof (' Mac ') >-1,//whether for iphone or Qqhd browser
Ipad:u.indexof (' ipad ') >-1,//whether the ipad
Webapp:u.indexof (' Safari ') = = 1/Whether the Web should program, without head and bottom
};
}()
}

Document.writeln ("Whether for Mobile Terminal:" +browser.versions.mobile);
Document.writeln ("iOS terminal:" +browser.versions.ios);
Document.writeln ("Android Terminal:" +browser.versions.android);
Document.writeln ("Whether for IPhone:" +browser.versions.iphone);
Document.writeln ("Whether the IPad:" +browser.versions.ipad);
Document.writeln (navigator.useragent);
</script>
</coding>


JavaScript, whether it's a PC browser or a mobile browser, is judged by the user Agent.

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.