_javascript Techniques for conditional compilation of jscript/javascript in IE

Source: Internet
Author: User
Tags comment tag numeric try catch

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
  1. <script type= "Text/javascript" >
  2. /* @cc_on
  3. document.write ("JScript version:" + @_jscript_version + ".<br>");
  4. /* @if (@_jscript_version >= 5)
  5. document.write ("JScript version 5.0+.<br \/>");
  6. document.write ("Only when browsers support jscript5+ can you see these words .<br>");
  7. @else @*/
  8. Document. Write("When you use other browsers (such as Firefox, IE 4.x, etc.) to see this line of text <br>");
  9. / * @end
  10. @*/
  11. </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:

    • @if
    • @elif
    • @else
    • @end

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
    1. /* @cc_on
    2. @if (@_win32)
    3. document.write (The operating system is 32-bit Windows. The browser is IE. ");
    4. @else
    5. document.write (the operating system is not 32-bit Windows.) The browser is IE. ");
    6. @end
    7. @*/

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
    1. /* @cc_on
    2.    /* @if (@_win32)
    3.       document.write ("Operating system is 32-bit Windows." The browser is IE. ");
    4.     @else @*/
    5.       document. write browsers are not IE (such as Firefox) or browsers that are not in 32-bit Windows. "
    6.     /* @end
    7. @*/
    8. &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
  1. /* @cc_on
  2. @if (@_jscript_version >= 5)
  3. document.write ("IE Browser that supports JScript 5+");
  4. @elif (@_jscript_version >= 4)
  5. document.write ("IE Browser that supports JScript 4+");
  6. @else
  7. document.write ("Very old IE Browser");
  8. @end
  9. @*/

If, ElseIf, Else Logic 2 (contains other browsers)

Language:javascript, parsed in:0.004 seconds, using GeSHi 1.0.7.12
  1. /* @cc_on
  2. /* @if (@_jscript_version >= 5)
  3. document.write ("IE Browser that supports JScript 5+");
  4. @elif (@_jscript_version >= 4)
  5. document.write ("IE Browser that supports JScript 4+");
  6. @else @*/
  7. Document. Write("Non IE Browser (one that doesn ' t support JScript)");
  8. / * @end
  9. @*/

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
    1. /* @cc_on
    2. @if (@_win32)
    3. document.write ("OS is 32-bit.") Browser is IE. ");
    4. @else
    5. document.write ("OS is not 32-bit.") Browser is IE. ");
    6. @end
    7. @*/

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
  1. @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
  1. @set @myvar1 = km
  2. @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
  1. @ If (@newVar!= @newVar)
  2. 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
  1. function HttpRequest(URL, parameters){
  2. var pagerequest = false //variable to hold Ajax object
  3. if (window. XMLHttpRequest) //if Mozilla, Safari etc
  4. Pagerequest = new XMLHttpRequest()
  5. Else if (window. ActiveXObject){ //if IE
  6. try {
  7. Pagerequest = new ActiveXObject("Msxml2.xmlhttp")
  8. }
  9. catch (e){
  10. try{
  11. Pagerequest = new ActiveXObject("Microsoft.XMLHTTP")
  12. }
  13. catch (e){}
  14. }
  15. }
  16. Else
  17. return false
  18. }

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
  1. function HttpRequest(URL, parameters){
  2. var pagerequest = false //variable to hold Ajax object
  3. /* @cc_on
  4. @if (@_jscript_version >= 5)
  5. try {
  6. Pagerequest = new ActiveXObject ("Msxml2.xmlhttp")
  7. }
  8. catch (e) {
  9. try {
  10. Pagerequest = new ActiveXObject ("Microsoft.XMLHTTP")
  11. }
  12. catch (E2) {
  13. Pagerequest = False
  14. }
  15. }
  16. @end
  17. @*/
  18. If (!pagerequest && typeof xmlhttprequest!= ' undefined ')
  19. Pagerequest = new XMLHttpRequest()
  20. }

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.) )

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.