JavaScript determines browser type

Source: Internet
Author: User

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

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

Here's how to use it:

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

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

Here's how to differentiate between IE6, IE7, and IE8:

varisie=!!window. ActiveXObject;varisie6=isie&&!window. XMLHttpRequest;varisie8=isie&&!!Document.documentmode;varisie7=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 for IE's case, carried out at once the detection, if you have doubts about this, you can test it.
I use it directly in my judgments, and you can use them to declare them as variables first. It is said that Firefox will also join document.all this way, so it is recommended to use the second method, it should be more secure.
Using Navigator.userAgent.indexOf () to differentiate multiple browsers, the code examples are as follows:

<HTML>  <Head>    <title>TAG Index Web</title>  </Head>  <Body>  <Scripttype= "Text/javascript">document.write ('Browser Award:'); //contains "opera" text columns    if(Navigator.userAgent.indexOf ("Opera") != -1) {document.write ('your browser when opera bar? '); }    //contains "msie" text columns    Else if(Navigator.userAgent.indexOf ("MSIE") != -1) {document.write ('is Internet Explorer in your browser? '); }    //contains "firefox" text columns    Else if(Navigator.userAgent.indexOf ("Firefox") != -1) {document.write ('your browser when Firefox bar? '); }    //contains "netscape" text columns    Else if(Navigator.userAgent.indexOf ("Netscape") != -1) {document.write ('is your browser Netscape? '); }    //contains "safari" text columns    Else if(Navigator.userAgent.indexOf ("Safari") != -1) {document.write ('your browser when Safari bar? '); }Else{document.write ('Unrecognized browser. '); } document.write ("'); </Script>  </Body></HTML>

Transferred from: http://hi.baidu.com/sqq_qqs/item/b0c508c2e960193c0831c676

JavaScript determines browser type

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.