1. Computing

Identifying Internet Explorer

Join the Discussion

Questions? Comments?

Internet Explorer is somewhat different from other web browsers when it comes to client side scripting. Where other web browsers support Javascript as their client side scripting language, Internet Explorer instead supports VBScript as well as JScript (which is similar but not identical to Javascript.

What this means is that while a lot of simple Javascript code can also be correctly run as JScript and feature sensing can allow us to choose between the Javascript and JScript equivalents there is also a lot of JScript functionality that will cause browsers other than Internet Explorer to crash unless a significant amount of extra code is included to handle browser differences. At least that is what it would mean if there weren't an easy way of setting up code that will only run on Internet Explorer.

Attempting to tell which browser is being used based on the information that the browser provides via fields such as the userAgent can be rather extensive since many alternative web browsers can attempt to disguise themselves as Internet Explorer in an attempt to bypass the simpler tests that web novices mistakenly use instead of testing if the required features are supported. There are however two much simpler ways of easily telling clearly whether the browser is Internet Explorer or not in a very few lines of code using a form of feature sensing.

The first way to test if a browser is Internet Explorer or not is to make use of the fact that only Internet Explorer can run VBScript. We can therefore use a combination of code in different languages to set a variable that will identify whether or not the browser is IE.

<script language="javascript">var ie = 0; </script>
<script language="vbscript">ie = 1</script>

This code makes use of the language attribute on the script tag to tell the browser that the second assignment statement is VBScript. That statement will therefore be ignored by all browsers except Internet Explorer since they don't understand that language. All of the browsers except Internet Explorer understand Javascript and so the first statement gets run in those browsers. IE treats references to Javascript as JScript and so runs both statements. As a result we now have a variable that contains '1' if the browser is Internet Explorer and '0' if it isn't.

The only problem with this code is that the latest HTML standards state that the language attribute is deprecated on the script tag and therefore should not be used as the next version of XHTML does not support it. Fortunately Internet Explorer provides an alternative method of producing the same effect that will allow us to avoid this problem.

<script type="text/javascript">var ie = 0;</script>
<!--[if IE]>
<script type="text/javascript">ie = 1;</script>
<![endif]-->

This version uses Javascript for both statements. Instead of stopping the second statement from running in browsers other than IE by using a language that other browsers don't understand this code instead uses HTML comments written in a special format that Internet Explorer interpret as a special HTML IF statement. As the statement does not specify a particular browser version the enclosed code is run by all 5+ versions of Internet Explorer as version 5 was when this coding was introduced (almost no one is running a version of IE older than that). Since other browsers treat the HTML as nothing but comments the second script does not run in those browsers. This means that again we have a variable that is set to 1 if the browser is IE and 0 if it is a different browser and we are not using any deprecated attributes.

©2014 About.com. All rights reserved.