Conditional compilation of jscript/javascript in IE
- Author: JavaScript Kit
- Translator: Sheneyan)
- Translation Date:
- Original article: Conditional Compilation of JScript/JavaScript in IE
- Copyright: translation is only responsible for the Chinese part I have translated without the consent of javascript kit. Copyright belongs to the original author.
Conditional compilation Overview
In IE, there is a little-known function calledConditional compilation. Since IE4 began to support this function, it has received some attention due to the emergence of some Ajax-related javascript scripts. Conditional compilation is an independent form of object judgment, so that IE can determine whether to compile a specific part of your jscript or javascript code based on predefined or user-defined conditions. You can also think of it as a condition comment for your code, so that your code can run smoothly on non-IE browsing.
Syntax Overview
By using@ Cc_on
To activate Conditional compilation, or directly use@ If
Or@ Set
And so on. Here is an 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 ("You can see the text only when the browser supports JScript5 +. <br> ");
- @ Else @*/
- Document.Write("You can see this line of text in other browsers (such as Firefox, IE 4.x) <br>");
- /* @ End
- @*/
-
- </Script>
-
Example: JScript 5.6.
JScript version 5.0 +.
Only when the browser supports JScript5 + can you see the text.
If you use IE (any version), you should be able to see the firstDocument. write ()
For IE5 +, the next twoDocument. write ()
You can also see (because JScript 5 is supported from IE5 ). LastDocument. write ()
The method is for other non-IE5 + browser services, either Firefox, opera, IE4, or anything else. Conditional compilation relies on Annotation labels similar to those used in condition annotations to ensure that it works smoothly in all browsers.
When using Conditional compilation, it is best to first pass@ Cc_on
Statement to activate it. Only in this way can you include annotation labels in your script to ensure browser compatibility, as shown in the preceding example. (Zi Wu Note: I have not translated this sentence very smoothly... it seems that it is in conflict with the above sentence.)
@ If, @ elif, @ else, @ end statement
After this strange opening remark, here are some conditional statements for condition convenience:
Now let's look at some "weird" examples.
If else logic (excluding browsers other than IE) 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 a 32-bit windows. The browser is IE. ");
- @ End
- @*/
-
This is a complete script that is only recognized by IE and ignored by all other browsers. This script will display different contents on different operating systems. Compare the following example with the following example ......
If else logic 2 (including other browsers) Language: javascript, parsed in: 0.004 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 browser is not IE (for example, Firefox) or IE in 32-bit windows. ");
- /* @ End
- @*/
-
Familiar with annotation labels. In this exampleElse
Some can contain all non-IE browsers (such as firefox) and IE in non-32-bit windows. After studying this comment, you will understand the logic until your head is dizzy :)
If, elseif, else logic (excluding browsers outside IE)
Continue. You can view all the content:
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 (including 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 processing. In the last exampleElse
The statement contains all non-ie browsers.
Conditional compilation Variables
In the previous section, you saw some strange variables, such@ _ Win32
. Here are some predefined condition compilation variables that you can use to determine the general description of IE or computers:
Predefined Conditional compilation Variables
Variable |
Description |
@ _ Win32 |
Returned when running in a win32 SystemTrueOtherwise, returnNaN. |
@ _ Win16 |
Returned when running in a win16 SystemTrueOtherwise, returnNaN. |
@ _ Mac |
Returned when running in an Apple Macintosh SystemTrueOtherwise, returnNaN. |
@ _ Alpha |
Returned when running on the DEC aplha ProcessorTrueOtherwise, returnNaN. |
@ _ X86 |
Returned when running on an Intel processorTrueOtherwise, returnNaN. |
@ _ Mc680x0 |
When running on a Motorola 680x0 ProcessorTrueOtherwise, returnNaN. |
@ _ PowerPC |
When running on a Motorola PowerPC ProcessorTrueOtherwise, returnNaN. |
@ _ Jscript |
Always returnTrue. |
@ _ Jscript_build |
Number of JScript engine compilations. |
@ _ Jscript_version |
Jscript version,Major version. Minor versionFormat display. IE4 supports JScript 3.x IE5.x supports JScript 5.5- IE6 supports JScript 5.6 In JScript.net, the number of versions is 7.X. |
@ _ Debug |
If it is compiled in debug mode, returnTrueOtherwise, returnFalse. |
@ _ Fast |
If compiled in fast mode, returnTrueOtherwise, returnFalse. |
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. The syntax is as follows:
Language: javascript, parsed in: 0.001 seconds, using GeSHi 1.0.7.12
- @ Set @ varname = term
-
In Conditional compilation,NumberAndBooleanType variables can be used,Character TypeUnavailable. For example:
Language: javascript, parsed in: 0.002 seconds, using GeSHi 1.0.7.12
- @ Set @ myvar1 =35
- @ Set @ myvar3 = @ _ jscript_version
-
Standard operators can be used in Conditional compilation logic:
- ! ~
- */%
- +-
- <>>>>
- <=> =
- =! ===! =
- & ^ |
- & |
You can determine whether to returnNaN
To determine whether a user-defined variable is defined:
Language: javascript, parsed in: 0.002 seconds, using GeSHi 1.0.7.12
- @If (@ NewVar! = @ NewVar)
- // The variable is not defined.
-
BecauseNaN
Is the only one that is not equal to its own value, so this script can run normally.
Conditional compilation example -- try catch statement
At the beginning of the tutorial, I once mentioned how Conditional compilation can be used in some Ajax JavaScript to show its boast. Now I want to tell you what I mean. An Ajax script usually contains a central function used 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
- FunctionHttpRequest(Url, parameters){
- VarPageRequest =False // Variable to hold ajax object
- If (Window.XMLHttpRequest) // If Mozilla, Safari etc
- PageRequest =NewXMLHttpRequest()
- Else If (Window.ActiveXObject){ // If IE
- Try {
- PageRequest =NewActiveXObject("Msxml2.XMLHTTP")
- }
- Catch (E){
- Try{
- PageRequest =NewActiveXObject("Microsoft. XMLHTTP")
- }
- Catch (E){}
- }
- }
- Else
- Return False
- }
-
Many people thinkTry/catch
The statement can successfully test Ajax support. Unfortunately, this is not true. UnsupportedThrow/catch
Browsers such as IE 4.xactually block the above Code and return an error. To overcome this problem, Conditional compilation can be used to rough down a really cross-browser friendly Ajax handler:
Real cross-browser functions: Language: javascript, parsed in: 0.008 seconds, using GeSHi 1.0.7.12
- FunctionHttpRequest(Url, parameters){
- VarPageRequest =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 &&TypeofXMLHttpRequest! ='Undefined')
- PageRequest =NewXMLHttpRequest()
- }
-
Use Conditional compilation, completeTry/catch
The block is only used for IE5 +, and other browsers, such as IE4 or non-ie browsers, try to crack it (dicipher it... What is this dicipher? The Explanation of "Deciphering" is google. Does it seem better to translate it into "Ignore ?). Obviously, Firefox will continue and use XMLHttpRequest instead. Now you get it-a real cross-browser ajax function! (Zi wunote: In another article I translated, I can see a more comprehensive way to write this function.)