Conditional compilation of jscript/javascript in IE
- Author: JavaScript Kit
- Translator: Zou (Sheneyan)
- Translation Date: 2006-02-12
- English Original: Conditional compilation of Jscript/javascript in IE
- Copyright: I am only responsible for the Chinese part I have translated without the permission of JavaScript kit. Copyright belongs to the original author.
Conditional Compilation Overview
In IE, there is a little-known feature called Conditional compilation. Since IE4 began to support this feature, it has received some attention due to its presence in some Ajax-related JavaScript scripts. Conditional compilation, as an independent form of object judgment, allows IE to determine whether your JScript or JavaScript code-specific parts are compiled based on predefined or user-defined conditions. You can also think of it as a conditional annotation of your code so that your code can run smoothly on non ie browsing.
Syntax overview
Activate conditional compilation by using it in your script @cc_on
, or use @if
it directly or as a @set
part of the CC logic. Here is a sample example:
Language:javascript, parsed in:0.007 seconds, using GeSHi 1.0.7.12
-
<script type= "Text/javascript" >
-
-
/* @cc_on
-
document.write ("JScript version:" + @_jscript_version + ".<br>");
-
/* @if (@_jscript_version >= 5)
-
document.write ("JScript version 5.0+.<br \/>");
-
document.write ("Only when browsers support jscript5+ can you see these words .<br>");
-
@else @*/
-
Document. Write("When you use other browsers (such as Firefox, IE 4.x, etc.) to see this line of text <br>");
-
/ * @end
-
@*/
-
-
</script>
-
Example:
JScript version: 5.6.
JScript version 5.0+.
You can only see these words when the browser supports jscript5+.
If you use IE (any version), you should be able to see document.write()
the first output, and if it is ie5+, you will be able to see the next two document.write()
(since JScript 5 is supported from IE5). The final document.write()
approach is to serve other ie5+ browsers, either firefox,opera,ie4 or anything else. Conditional compilation relies on annotation labels similar to those used in conditional comments to ensure that it works smoothly in all browsers.
When using conditional compilation, it is best to @cc_on
activate it with a statement so that you can include a comment tag in your script to ensure that the browser is compatible, as shown in the previous example. ( Zi Wu Note: This sentence English I translate is not very smooth ... It seems to contradict the above sentence)
@if, @elif, @else, @end statement
After this strange opening, here are some conditional statements for easy conditions:
Now let's look at some "odd" examples.
If else logic (excluding IE browsers)
Language:javascript, parsed in:0.001 seconds, using GeSHi 1.0.7.12
-
/* @cc_on
-
@if (@_win32)
-
document.write (The operating system is 32-bit Windows. The browser is IE. ");
-
@else
-
document.write (the operating system is not 32-bit Windows.) The browser is IE. ");
-
@end
-
@*/
-
This is a complete script that is identified by IE and ignores all other browsers, and this script displays different content on different operating systems. Compare the following example ...
If Else logic 2 (contains other browsers)
Language:javascript, parsed in:0.004 seconds, using GeSHi 1.0.7.12
-
/* @cc_on
- /* @if (@_win32)
-
document.write ("Operating system is 32-bit Windows." The browser is IE. ");
-
@else @*/
-
document. write browsers are not IE (such as Firefox) or browsers that are not in 32-bit Windows. "
-
/* @end
-
@*/
-
&NB Sp
Proficiency in the use of annotation tags, this example else
can include all non-IE browsers (such as Firefox), and not 32-bit Windows under the IE. Work this note until your head is dizzy and you'll understand the logic:
If, ElseIf, else logic (excluding IE browsers)
Go on, you can see the whole story:
Language:javascript, parsed in:0.001 seconds, using GeSHi 1.0.7.12
-
/* @cc_on
-
@if (@_jscript_version >= 5)
-
document.write ("IE Browser that supports JScript 5+");
-
@elif (@_jscript_version >= 4)
-
document.write ("IE Browser that supports JScript 4+");
-
@else
-
document.write ("Very old IE Browser");
-
@end
-
@*/
-
If, ElseIf, Else Logic 2 (contains other browsers)
Language:javascript, parsed in:0.004 seconds, using GeSHi 1.0.7.12
-
/* @cc_on
-
/* @if (@_jscript_version >= 5)
-
document.write ("IE Browser that supports JScript 5+");
-
@elif (@_jscript_version >= 4)
-
document.write ("IE Browser that supports JScript 4+");
-
@else @*/
-
Document. Write("Non IE Browser (one that doesn ' t support JScript)");
-
/ * @end
-
@*/
-
Comprehensive treatment. In this last example, the last else
statement contains all non-IE browsers.
Conditional Compilation variables
In the previous section, you saw some strange variables like @_win32
. Here are some predefined conditional compilation variables that you can use to determine IE or the approximate description of a computer:
predefined conditional Compilation variables
variable |
Description |
@_win32 |
Returns truewhen running in a Win32 system, otherwise it returns NaN. |
@_win16 |
Returns true when running in a Win16 system, otherwise it returns NaN. |
@_mac |
Returns true when running on an Apple Macintosh system, otherwise returns NaN. |
@_alpha |
Returns true when running on the DEC Aplha processor, otherwise returning NaN. |
@_x86 |
Returns true when running on an Intel process, otherwise it returns NaN. |
@_mc680x0 |
true when running on the Motorola 680x0 processor, otherwise return NaN. |
@_powerpc |
true when running on the Motorola PowerPC processor, otherwise return NaN. |
@_jscript |
Returns trueforever. |
@_jscript_build |
The number of times the JScript script engine compiles. |
@_jscript_version |
A JScript version that is displayed in a major version, minor version format. IE4 supports JScript 3.x ie5.x supports JScript 5.5- IE6 supports JScript 5.6 In JScript.NET, this version number is 7. x. |
@_debug |
Returns true if compiled in debug mode, otherwise returns false. |
@_fast |
Returns true if compiled in fast mode, otherwise returns false. |
In most cases, you may only need to use @_win
and @jscript_build
:
Language:javascript, parsed in:0.001 seconds, using GeSHi 1.0.7.12
-
/* @cc_on
-
@if (@_win32)
-
document.write ("OS is 32-bit.") Browser is IE. ");
-
@else
-
document.write ("OS is not 32-bit.") Browser is IE. ");
-
@end
-
@*/
-
User-defined variables
You can also define your own variables in the conditional compilation block, and the syntax is as follows:
Language:javascript, parsed in:0.001 seconds, using GeSHi 1.0.7.12
-
@set @varname = term
-
In conditional compilation, numeric and boolean-type variables can be used, but character types cannot be used. For example:
Language:javascript, parsed in:0.002 seconds, using GeSHi 1.0.7.12
-
@set @myvar1 = km
-
@set @myvar3 = @_jscript_version
-
The standard operators can be used in conditional compilation logic:
- ! ~
- * / %
- + -
- << >> >>>
- < <= > >=
- == != === !==
- & ^ |
- && |
You can NaN
determine whether a user-defined variable is defined by determining whether to return it:
Language:javascript, parsed in:0.002 seconds, using GeSHi 1.0.7.12
-
@ If (@newVar!= @newVar)
-
The variable is not defined
-
Because it NaN
is the only value that is not equal to itself, this script works correctly.
Conditional compilation Example--try catch statement
At the beginning of the tutorial, I used to mention how conditional compilation shows its worth boasting in the presence of some Ajax JavaScript. Now I'm going to tell you what I mean. An AJAX script typically contains a central function to determine the support of browsers (ie, FF, etc.) for generating asynchronous request objects:
Typical Ajax functions:
Language:javascript, parsed in:0.020 seconds, using GeSHi 1.0.7.12
-
function HttpRequest(URL, parameters){
-
var pagerequest = false //variable to hold Ajax object
-
if (window. XMLHttpRequest) //if Mozilla, Safari etc
-
Pagerequest = new XMLHttpRequest()
-
Else if (window. ActiveXObject){ //if IE
-
try {
-
Pagerequest = new ActiveXObject("Msxml2.xmlhttp")
-
}
-
catch (e){
-
try{
-
Pagerequest = new ActiveXObject("Microsoft.XMLHTTP")
-
}
-
catch (e){}
-
}
-
}
-
Else
-
return false
-
}
-
Many people think that the try/catch
statement can be a smooth test of Ajax support, unfortunately, this is not true. Browsers that are not supported, throw/catch
such as IE 4.x, actually block the code above and return an error. To overcome this problem, conditional compilation can be used to reduce a truly cross-browser friendly Ajax processing function:
True Cross-browser functions:
Language:javascript, parsed in:0.008 seconds, using GeSHi 1.0.7.12
-
function HttpRequest(URL, parameters){
-
var pagerequest = false //variable to hold Ajax object
-
/* @cc_on
-
@if (@_jscript_version >= 5)
-
try {
-
Pagerequest = new ActiveXObject ("Msxml2.xmlhttp")
-
}
-
catch (e) {
-
try {
-
Pagerequest = new ActiveXObject ("Microsoft.XMLHTTP")
-
}
-
catch (E2) {
-
Pagerequest = False
-
}
-
}
-
@end
-
@*/
-
-
If (!pagerequest && typeof xmlhttprequest!= ' undefined ')
-
Pagerequest = new XMLHttpRequest()
-
}
-
Use conditional compilation, complete try/catch
block only for ie5+, the rest of the browser, such as IE4 or non-IE browser try to decipher it (dicipher it ...) What is this dicipher? "Deciphering" This explanation is Google to, personal feeling translates to "ignore" seems better? )。 The obvious Firefox will continue and use XMLHttpRequest instead. And now you've got it--a really cross-browser ajax function! ( Sub-note: In another article I translate, you can see this function in a more comprehensive way.) )