JavaScript determines whether the access source is a mobile phone or a computer. Which browser is used? _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to use JavaScript to determine whether the access source is a mobile phone or a computer, and which browser is used. For more information, see. Js determines what type of Browser

The Code is as follows:


If (window. sidebar & "object" = typeof (window. sidebar) & "function" = typeof (window. sidebar. addPanel) // firefox

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

{
}


Js is used to distinguish IE from other browsers and methods between IE6-8.

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

The usage is as follows:

If (document. all ){
Alert ("IE browser ");
} Else {
Alert ("non-IE browser ");
}

If (!! Window. ActiveXObject ){
Alert ("IE browser ");
} Else {
Alert ("non-IE browser ");
}

The following describes how to differentiate IE6, IE7, and IE8:

Var isIE = !! Window. ActiveXObject;
Var isIE6 = isIE &&! Window. XMLHttpRequest;
Var isIE8 = isIE &&!! Document.doc umentMode;
Var isIE7 = isIE &&! IsIE6 &&! IsIE8;
If (isIE ){
If (isIE6 ){
Alert ("ie6 ″);
} Else if (isIE8 ){
Alert ("ie8 ″);
} Else if (isIE7 ){
Alert ("ie7 ″);
}
}

First, we make sure that this browser is IE and has been tested once. If you have doubts about this, you can test it.

I am using it directly here. You can declare them as variables for use. It is said that Firefox will also add the document. all method in the future. Therefore, it is recommended to use the second method, which should be safer.

Use navigator. userAgent. indexOf () to differentiate multiple browsers. The code example is as follows:

The Code is as follows:



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.