View Full Version : IE version testing from within your script

02-13-2012, 09:08 PM
Most browsers change very little from one version to the next and have almost all of their users upgrade failrly quickly to the latest version. The exception is Internet Explorer which has major differences between versions and also has a large number of people using old versions. While IE9 siupports mostly the same JavaScript as all the other browsers do, older versions of IE are missing various JavaScript commands or can only use the JScript or ActiveX equivalents.

One way to distinguish the different versions is by using the conditional comments in the HTML that Microsoft supply that allow you to specify which version of Internet Explorer or Microsoft Office that the various pireces of the code are for. With the HTML conditional comments you can specify the version easily and target IE7 or MSO9 simply by specifying that in the conditional comment.

This isn't much help for those rare occasions where you need to know which version of Internet Explorer it is just for processing one small part of your script. While JScript also has conditional comments the versions you can test for from there are which version of JScript is supported rather than which version of the browser it is. You would need to track down which JScript versions correspond to which browser versions in order to code the test entirely with conditional comments in JScript.

The following function uses a slightly easier approach by just using a conditional comment to test if the browser is IE (since that's the only browser to support JScript conditional comments. It then tests for one of the new objects that was added in each version of IE to work out which browser it is - IE9 added support for eventListeners, IE8 added the JSON object and IE7 added the XMLHttpRequest object so the version of IE can be determined based on which of those objects the browser recognises.

ieVer = function() {
@if (@_jscript)
if (window.addEventListener) return 'IE9+';
if (JSON) return 'IE8';
if (typeof XMLHttpRequest != 'undefined') 'return 'IE7';
return 'IE6-';
return 'not IE';