犀牛书
Conditional Comments in Internet Explorer
In practice, you'll find that many of the incompatibilities in client-side JavaScript programming turn out to be IE-specific. That is, you must write code in one way for IE and in another way for all other browsers. Although you should normally avoid browser-specific extensions that are not likely to be standardized, IE supports conditional comments in both HTML and JavaScript code that can be useful.
Here is what conditional comments in HTML look like. Notice the tricks played with the closing delimiter of HTML comments:
<!--[if IE]>
This content is actually inside an HTML comment.
It will only be displayed in IE.
<![endif]-->
<!--[if gte IE 6]>
This content will only be displayed by IE 6 and later.
<![endif]-->
<!--[if !IE]> <-->
This is normal HTML content, but IE will not display it
because of the comment above and the comment below.
<!--> <![endif]-->
This is normal content, displayed by all browsers.
Conditional comments are also supported by IE's JavaScript interpreter, and C and C++ programmers may find them similar to the #ifdef/#endif functionality of the C preprocessor. A JavaScript conditional comment in IE begins with the text /*@cc_on and ends with the text @*/. (The cc in cc_on stands for conditional compilation.) The following conditional comment includes code that is executed only in IE:
/*@cc_on
@if (@_jscript)
// This code is inside a JS comment but is executed in IE.
alert("In IE");
@end
@*/
Inside a conditional comment, the keywords @if, @else, and @end delimit the code that is to be conditionally executed by IE's JavaScript interpreter. Most of the time, you need only the simple conditional shown above: @if (@_jscript). JScript is Microsoft's name for its JavaScript interpreter, and the @_jscript variable is always true in IE.
With clever interleaving of conditional comments and regular JavaScript comments, you can set up one block of code to run in IE and a different block to run in all other browsers:
/*@cc_on
@if (@_jscript)
// This code is inside a conditional comment, which is also a
// regular JavaScript comment. IE runs it but other browsers ignore it.
alert('You are using Internet Explorer);
@else*/
// This code is no longer inside a JavaScript comment, but is still
// inside the IE conditional comment. This means that all browsers
// except IE will run this code.
alert('You are not using Internet Explorer');
/*@end
@*/
Conditional comments, in both their HTML and JavaScript forms, are completely nonstandard. They are sometimes a useful way to achieve compatibility with IE, however.
Conditional Comments in Internet Explorer
In practice, you'll find that many of the incompatibilities in client-side JavaScript programming turn out to be IE-specific. That is, you must write code in one way for IE and in another way for all other browsers. Although you should normally avoid browser-specific extensions that are not likely to be standardized, IE supports conditional comments in both HTML and JavaScript code that can be useful.
Here is what conditional comments in HTML look like. Notice the tricks played with the closing delimiter of HTML comments:
<!--[if IE]>
This content is actually inside an HTML comment.
It will only be displayed in IE.
<![endif]-->
<!--[if gte IE 6]>
This content will only be displayed by IE 6 and later.
<![endif]-->
<!--[if !IE]> <-->
This is normal HTML content, but IE will not display it
because of the comment above and the comment below.
<!--> <![endif]-->
This is normal content, displayed by all browsers.
Conditional comments are also supported by IE's JavaScript interpreter, and C and C++ programmers may find them similar to the #ifdef/#endif functionality of the C preprocessor. A JavaScript conditional comment in IE begins with the text /*@cc_on and ends with the text @*/. (The cc in cc_on stands for conditional compilation.) The following conditional comment includes code that is executed only in IE:
/*@cc_on
@if (@_jscript)
// This code is inside a JS comment but is executed in IE.
alert("In IE");
@end
@*/
Inside a conditional comment, the keywords @if, @else, and @end delimit the code that is to be conditionally executed by IE's JavaScript interpreter. Most of the time, you need only the simple conditional shown above: @if (@_jscript). JScript is Microsoft's name for its JavaScript interpreter, and the @_jscript variable is always true in IE.
With clever interleaving of conditional comments and regular JavaScript comments, you can set up one block of code to run in IE and a different block to run in all other browsers:
/*@cc_on
@if (@_jscript)
// This code is inside a conditional comment, which is also a
// regular JavaScript comment. IE runs it but other browsers ignore it.
alert('You are using Internet Explorer);
@else*/
// This code is no longer inside a JavaScript comment, but is still
// inside the IE conditional comment. This means that all browsers
// except IE will run this code.
alert('You are not using Internet Explorer');
/*@end
@*/
Conditional comments, in both their HTML and JavaScript forms, are completely nonstandard. They are sometimes a useful way to achieve compatibility with IE, however.