If browser == IE6{
alert('hi');
}
views:
566answers:
8 var ua = window.navigator.userAgent;
var msie = ua.indexOf ( "MSIE " );
if ( msie > 0 ) // If Internet Explorer, return version number
return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )));
else // If another browser, return 0
return 0;
Conditional comments are a good alternative:
<!--[if IE 6]>
var everythingIsBroken = true;
<![endif]-->
Don't base your detection on the user-agent. There are plenty of other browsers out there that use the Trident 4 engine (the one IE6 use) that are not IE6.
The answer is simple: don't detect the browser, detect the engine. To do that, you must use what's called feature based detection.
Using feature based detection has the following advantages:
- Detects all browsers using similar rendering engine than target.
- Easier code branching to work around issues of a rendering engine.
- Less false positives (UAs can be modified to pass as another browser, features can't).
The following script uses browser features to detect the engine. Credit to The MooTools production team (http://mootools.net/developers/).
Note: The snippet below has been modified to work without the MooTools javascript framework. If you do wish to work with MooTools, you no longer need this code, it is part of the distribution.
function $tryCatch(){
for (var i = 0, l = arguments.length; i < l; i++){
try {
return arguments[i]();
} catch(e){}
}
return null;
};
var Browser = {
Engine: {name: 'unknown', version: 0},
Platform: {name: (window.orientation != undefined) ? 'ipod' : (navigator.platform.match(/mac|win|linux/i) || ['other'])[0].toLowerCase()},
Features: {xpath: !!(document.evaluate), air: !!(window.runtime), query: !!(document.querySelector)},
Plugins: {},
Engines: {
presto: function(){
return (!window.opera) ? false : ((arguments.callee.caller) ? 960 : ((document.getElementsByClassName) ? 950 : 925));
},
trident: function(){
return (!window.ActiveXObject) ? false : ((window.XMLHttpRequest) ? ((document.querySelectorAll) ? 6 : 5) : 4);
},
webkit: function(){
return (navigator.taintEnabled) ? false : ((Browser.Features.xpath) ? ((Browser.Features.query) ? 525 : 420) : 419);
},
gecko: function(){
return (!document.getBoxObjectFor && window.mozInnerScreenX == null) ? false : ((document.getElementsByClassName) ? 19 : 18);
}
}
};
Browser.Platform[Browser.Platform.name] = true;
Browser.detect = function(){
for (var engine in this.Engines){
var version = this.Engines[engine]();
if (version){
this.Engine = {name: engine, version: version};
this.Engine[engine] = this.Engine[engine + version] = true;
break;
}
}
return {name: engine, version: version};
};
Browser.detect();
Browser.Request = function(){
return $tryCatch(function(){
return new XMLHttpRequest();
}, function(){
return new ActiveXObject('MSXML2.XMLHTTP');
}, function(){
return new ActiveXObject('Microsoft.XMLHTTP');
});
};
Browser.Features.xhr = !!(Browser.Request());
Browser.Plugins.Flash = (function(){
var version = ($tryCatch(function(){
return navigator.plugins['Shockwave Flash'].description;
}, function(){
return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version');
}) || '0 r0').match(/\d+/g);
return {version: parseInt(version[0] || 0 + '.' + version[1], 10) || 0, build: parseInt(version[2], 10) || 0};
})();
function $exec(text){
if (!text) return text;
if (window.execScript){
window.execScript(text);
} else {
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script[(Browser.Engine.webkit && Browser.Engine.version < 420) ? 'innerText' : 'text'] = text;
document.head.appendChild(script);
document.head.removeChild(script);
}
return text;
};
Just include this JavaScript class and you can detect IE6 and any other browser using the Trident4 engine by doing the following:
if(Browser.Engine.trident4) {
alert('IE6 or similar...');
} elseif(Browser.Engine.name == "trident") {
alert('Internet Explorer Trident Rendering Engine Version ' + Browser.Engine.version);
}
What is the best way to detect <browser_x> using JavaScript?
By not.
As Andrew Moore has mentioned in comments in this post, you should be using feature detection. This will make your code more "future-proof". If another browser includes, or no longer supports a feature in the future, then your code will be safe. There are a number of sites out there explaining how to handle this. The concept is huge and covers a lot of concepts, so rather than writing an essay/book on this, here are some resources to use:
- Object Detection (start here)
- Feature Detection (or start here)
- Browser detection vs Object Detection
- jQuery.support - Great way to check for features if you already have jQuery included on a page.
if(!window.addEventListener && !window.XMLHttpRequest && window.attachEvent){
// It quacks like IE-6, but it could be 5.5.
}
I'm not sure if there's a special reason why you want to detect IE 6 but in general it is better to try to detect the browser's features rather than detecting the browser. You can do this easily with JQuery using jQuery.support: http://api.jquery.com/jQuery.support/.
At the risk of actually answering the question that was asked (and assuming for a moment the asker has a good reason to specifically detect IE6):
if (/\bMSIE 6/.test(navigator.userAgent) && !window.opera) {
// yep, browser claims to be IE6
}
Very late addition.
In addition to the HTML conditional comments used in apphacker's answer, microsoft's JScript implementation also provides conditional comments for javascript:
<script type="text/javascript">
var isIE6 = /*@cc_on/*@if(@_jscript_version<=5.6)1@else@*/0/*@end@*/;
if (isIE6) {
alert("You're screwed");
}
</script>
The good thing is that it can also be used in external .js
files.
For a table listing the version of JScript that are implemented by Microsoft host applications: http://msdn.microsoft.com/en-us/library/s4esdbwz%28VS.85%29.aspx