Jscript / javascript conditions compiler
Jscript in IE / javascript conditions compiler
Author: JavaScript Kit
Translator: Ukraine-(Sheneyan)
Translation: 2006-02-12
English Original: Conditional Compilation of JScript / JavaScript in IE
Copyright: Translation javascript kit has not been agreed to, I only on my part responsible for the Chinese translation. Copyright.
- Ukraine Note: Like the previous article, I can only according to my understanding of the article, translated, but my own level is limited, and many places there may be a misunderstanding of the original or even misunderstanding, if possible, or read the text.
Conditions outlined compiler
In IE, there is a little-known feature called conditions compiler (conditional compilation). Since IE4 begun to support this function, which in some Ajax related javascript script by the emergence of some of the concerns. Conditions compiler as an independent form of the object of judgement, can make IE predefined or user-defined conditions to determine your jscript or specific parts of javascript code compiler. It can also code as a condition of your Note (contional comments, will soon be translated this article), so that you can code in the non-IE browser is also running smoothly.
Grammar outlined
In your script through the use of conditions to activate @ cc_on compiler, or if used directly or @ @ CC set, and so on as part of the logic of the sentence to activate it. Here is a model example:
<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 the browser support JScript5 when you can see that these words. <Br>");
@ @ Else * /
Document.write ( "When you use other browsers (such as: Firefox, IE 4.x of) the time to see this text <br>");
/ * @ End
@ * /
</ Script>
Examples:
Operation code box
[Ctrl A full selection Note: You may first revision of the code, and then running]
If you use IE (any version), you should be able to see that a document.write () output, if it is IE5, the next two document.write () you can see (from IE5 because support for JScript 5 ). Finally a document.write () method to other non-IE5 browser services, whether it is Firefox, opera, IE4, or what else. Compiler dependent on conditions similar to the conditions used in the Notes Note label, to ensure that it is in all browsers can work smoothly.
When conditions compile time, the best first pass @ cc_on statement to activate it, the only way you can in your script contains Note label in order to ensure compatible browser, like the above example demonstrates. (A Ukrainian Note: This English translation is not, I am with … may seem contradictory with the above sentence)
If @, @ elif, @ else, @ end statements
In this strange prologue, here are some easy conditions for the conditional statements:
@ If
@ Elif
@ Else
@ End
Now let us look at some "strange" examples.
If else logic (excluded from the IE browser)
/ * @ Cc_on
@ If (@ _win32)
Document.write ( "operating system is windows 32. Browser is IE.");
@ Else
Document.write ( "windows 32 operating system is not. Browser is IE.");
@ End
@ * /
This is a complete script, ie only the browser to identify and ignore all other browsers, this script in different operating systems will have different content. Comparing this example……
If else Logic 2 (which includes other browsers)
/ * @ Cc_on
/ * @ If (@ _win32)
Document.write ( "operating system is windows 32. Browser is IE.");
@ @ Else * /
Document.write ( "IE browser is not (eg: Firefox) or browser is not in the 32 windows of IE.");
/ * @ End
@ * /
Notes skilled use of labels, in this case to include all else part of the non-ie browsers (such as firefox), as well as non-under 32 windows of IE. Notes to this research, laying the head until you, you will understand this logic of the
If, elseif, else logic (excluded from the IE browser)
Go on, can be seen all the contents:
/ * @ 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 (which includes other browsers)
/ * @ 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 one else statement contains all non-IE browsers.
Compiler variable conditions
In the part before you see some strange variables such as @ _ win32. This is something which you can be judged by IE or computer broadly described predefined conditions compiler variables:

In most cases, you may only need to use the @ @ _win and jscript_build:
/ * @ 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 block in the conditions defined in the compiler your own variables, the following syntax:
Varname = @ @ set term
Conditions in the compiler, the digitized (Numeric) and Boolean (Boolean) types of variables can be used, but the character (String) unusable. For example:
@ @ Set myvar1 = 35
Myvar3 = @ @ @ set _jscript_version
Compiler logic of the conditions to the use of standard computing Address:
! ~
* /%
–
<<>>>>>
<<=>> =
==! = ===! ==
& ^ |
& |
You can judge whether or not to return NaN through to determine whether a definition of user-defined variables:
@ If (@ newVar! = @ NewVar)
/ / The variable undefined
As NaN is the only range of its own value, and so this script can operate normally.
Examples of conditions compiler - try catch phrase
Guide in the beginning of the conditions I have mentioned how in some compiler Ajax in the emergence of the JavaScript shown its worth boast side. Now, I have to tell you I am referring to the contents. Ajax a script usually includes a central function used to determine the browser (ie, ff, etc.) to have the support of asynchronous request object:
Typical ajax function:
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 believe that try / catch phrase smooth testing Ajax support, unfortunately, this is not true. Those who do not support throw / catch browsers, such as IE 4.x will actually blocking above the code and return to a mistake. To overcome this problem, conditions that can be used to compile a rough trip by the genuine cross-browser friendly Ajax handling functions:
Truly cross-browser function:
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 ()
)
Conditions of use compiler, and complete try / catch block only for IE5, and the remaining browser, such as IE4 browser, IE or try to decipher it (dicipher dicipher it … what is this? "Decipher" the interpretation is google to the personal feeling translated into "ignore" seems to be better?). Firefox will continue to clear and replace the use of XMLHttpRequest. Now you have it - a truly cross-browser ajax function! (A Ukrainian Note: In my translation of another article, we can see that this function more comprehensive way.)
Tags: css javascript






Leave a Reply