HTML and CSS Reference
In-Depth Information
Conditional Comments with Internet Explorer
One challenge in creating cross-browser Web sites is the fact that Internet Explorer often
interprets code differently from other Web browsers. This was particularly true with
embedded audio and video clips for which Internet Explorer used ActiveX technology to
embed its multimedia player. Sometimes the easiest way to reconcile the two approaches
was to run one set of code for Internet Explorer and another set of code for the other
browsers. This could be done using conditional comments that employ the syntax
<!--[if condition IE version ]>
HTML code
<![endif]-->
where condition is a condition that is either true or false, version is the version
number of an IE browser, and HTML code is code that will be run if the condition is true.
If you want to run code only for Internet Explorer (regardless of the version), you would
enter the following code:
<!--[if IE]>
HTML code
<![endif]-->
If a non-IE browser encounters this code structure, it interprets the whole structure as
one long HTML comment and does not attempt to parse any of the HTML code within
the comment tags. You also can mark code to be run only if a browser is not Internet
Explorer using the following structure:
<![if !IE]>
HTML code
<![endif]>
One use of conditional comments was to create one opening <object> tag for Internet
Explorer involving the use of ActiveX, and a different opening <object> tag for every other
browser. For example, the following code uses conditional comments to create two possible
sets of <object> tags for IE and non-IE browsers in order to embed a Java applet:
<!--[if IE]>
<object type=”application/x-java-applet”
width=”260” height=”130”
classid=”clsid:8AD9C840-044E-11D1-B3E9-00805F499D93”>
<![endif]-->
<![if !IE]>
<object type=”application/x-java-applet”
width=”260” height=”130”>
<![endif]>
The main difference between the two <object> tags is that the Internet Explorer ver-
sion uses the classid attribute in order to access the ActiveX object for running Java.
Other browsers, not requiring ActiveX, do not require that attribute.
With more recent versions of Internet Explorer supporting more of the W3C standards
for CSS and HTML, and with the widespread adoption of HTML5, there is less of a need
for conditional comments than before. You may still encounter them when working with
the code for older Web pages, however.
Search WWH ::




Custom Search