Sometimes you want to add something to the page, but due to different browsers, the effect may not always be reflected. Now we can think of the browser’s judgment and give different results based on different browsers. The display effect is very dazzling, isn’t it? Of course, many people you can think of can think of it, just like the method of adding advertisements on Dachu.com, which really allowed me to see how a real JS master is trained. Without further ado, let’s look at the code:
if (window.XMLHttpRequest) { //Mozilla, Safari, IE7
alert('Mozilla, Safari,IE7 ');
if(!window.ActiveXObject){ // Mozilla, Safari,
alert('Mozilla, Safari');
} else {
alert('IE7');
}
} else {
alert('IE6');
}
Look below and transfer it online There is a JS script written by some talented people to judge IE and FF and various versions of IE IE6 IE7 IE8:
JS is used to distinguish IE from other browsers and between IE6-8.
1. document.all
2. !!window.ActiveXObject;
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");
}
Copy code
The code is as follows:
var isIE=!!window.ActiveXObject;
var isIE6=isIE&&!window.XMLHttpRequest;
var isIE8=isIE&&!! document.documentMode;
var isIE7=isIE&& !isIE6&&!isIE8;
if (isIE){
}
First we make sure that the browser is IE, It has been tested once, 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.
Statement: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