Home > Web Front-end > JS Tutorial > JavaScript determines whether the access is from a mobile phone or a computer, and which browser is used_javascript skills

JavaScript determines whether the access is from a mobile phone or a computer, and which browser is used_javascript skills

WBOY
Release: 2016-05-16 17:09:40
Original
1578 people have browsed it

js determines what type of browser it is

Copy the code 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 the method between IE6-8.

1. document.all
2. !!window.ActiveXObject;

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");
}

The following is how to distinguish 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 tested once when it is IE. If you have doubts about this, you can test it.

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

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

Copy the code The code is as follows:





JavaScript will pass User Agent to judge.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template