Aaron Gray wrote:
Hi,
Here's some new code I have not seen before it is used in WikiPedia as well
<!--[if lt IE 6]>
<style type="text/css" media="screen">
...
</style>
<![endif]-->
<!--[if IE 6]>
<style type="text/css" media="screen">
...
</style>
<![endif]-->
Does it work ?
How portable is it ?
What about old browsers ?
And 101 other questions :)
That's called "conditional comments" and they are introduced and
supported by Internet Explorer:
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/overview/ccomment_ovw.asp>
But it works just great for all browsers in the way it's made. Here's
the code from my test page template:
<!--[if gte IE 5]>
<p>
<var onclick="testIE()">Test IE</var>
</p>
<![endif]-->
<![if ! gte IE 5]>
<p>
<var onclick="testGK()">Test GK</var>
</p>
<![endif]>
If browser is Internet Explorer 4.x or higher it understands the
conditional comments and parses it properly. So if it's version is 5.0
or higher - [if gte IE 5] it displays
<var onclick="testIE()">Test IE</var>
otherwise it displays
<var onclick="testGK()">Test GK</var>
If it's another browser w/o conditional comments support (Firefox for
example) it simply skips the first block because it looks just like
regular <!-- ... --> HTML comment and it displays
<var onclick="testGK()">Test GK</var>
which is still exactly what we wanted.
A similar approach but for JavaScript code is implemented by Microsoft
in conditional compilation:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsconconditionalcompilation.asp>
Here is for example a block from my object dispatcher: depending on
what browser and what platform the script is running on, different
constructors are called:
/**
* Dispatching object request and degrade if needed
*/
/*@cc_on
@if (@_mac)
this.error.code = 1;
this.error.message = 'ActiveX is not supported on this platform.';
@elif (@_win32 && (@_jscript_version < 5))
this.error.code = 2;
this.error.message = 'JScript engine version is too low.';
@elif (@_win32 && (@_jscript_version >= 5))
jsFileManager_IE.call(this,mode);
@else @*/
if (typeof(netscape) == 'object') {
jsFileManager_GK.call(this,mode);
}
else {
this.error.code = 3;
this.error.message = 'XPConnect is not supported on this
platform.';
}
/*@end @*/
}
The beauty is that this works as needed with or without conditional
compilation support.