E has a little-known function called conditional compilation (conditional compilation). Since the IE4 began to support conditional compilation, this feature has gradually gained more attention and is used in its own JavaScript code, even in some Ajax code will often find its figure. Conditional compilation allows you to control whether IE browser executes your custom script section, but does not affect other browsers, and other browsers will only treat these custom scripts as normal script annotations.
If you use IE browser (any version), you will see that the first alert content is "JScript version: XX"
If you use IE5 and above browsers, you will see the second alert content is "JScript version 5.0 or higher".
If you use other browsers, such as Firefox, Opera,chrome, you'll only see a third alert.
Use conditional statements: @if, @elif, @else, @end
To attach a statement that declares a condition within a conditional compilation:
@if
@elif equivalent to ElseIf in other grammars
@else
@end
Let's look at a few examples of "odd":
If you use IE browser (any version), you will see that the first alert content is "JScript version: XX"
If you use IE5 and above browsers, you will see the second alert content is "JScript version 5.0 or higher".
If you use other browsers, such as Firefox, Opera,chrome, you'll only see a third alert.
Use conditional statements: @if, @elif, @else, @end
To attach a statement that declares a condition within a conditional compilation:
@if
@elif equivalent to ElseIf in other grammars
@else
@end
Let's look at a few examples of "odd":
In the last example, notice how the last else is written, and this non-IE browser also participates in the judgment.
Try catch ()
at the beginning of the first section, I have mentioned conditional compilation being used more and more in Ajax. Now I'm going to explain why. Different browsers provide asynchronous requests with different objects, resulting in a perfect Ajax program that must first determine the type and version of the current browser in the core function.
A typical example of an AJAX code can help you understand these:
function HttpRequest (URL, parameters) {
var pagerequest = false// Defines a variable that obtains an Ajax object and assigns an initial value (good habit) if (window). XMLHttpRequest//Mozilla systems such as Firefox, WebKit, Safari and Chrome, and opera support this object
Pagerequest = new XMLHttpRequest () else if ( Window. ActiveXObject) {//only IE support, and more than one try {pagerequest = new ActiveXObject ("Msxml2.xmlhttp")} catch (e) {try{= n EW ActiveXObject ("Microsoft.XMLHTTP")} catch (e) {}} else return false}
Most people think the script above can be executed successfully under IE. But unfortunately, that is not the case. Some browsers do not support "Throw/catch" browsers, such as the evil IE series of small four (ie 4.x). These browsers that do not support throwing the fetch will break and return an error before being thrown. To address this flaw, we use conditional compilation to rewrite the above code into an AJAX function that truly implements a reasonable Cross-browser operation:
An AJAX function that really implements a reasonable cross-browser operation