Conditional compilation of jscript/javascript in IE

Source: Internet
Author: User

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_onTo activate Conditional compilation, or directly use@ IfOr@ SetAnd so on. Here is an example:

Language: javascript, parsed in: 0.007 seconds, using GeSHi 1.0.7.12
  1. <Script type ="Text/javascript">
  2.  
  3. /* @ Cc_on
  4. Document. write ("JScript version:" + @ _ jscript_version + ". <br> ");
  5. /* @ If (@ _ jscript_version> = 5)
  6. Document. write ("JScript version 5.0 +. <br \/> ");
  7. Document. write ("You can see the text only when the browser supports JScript5 +. <br> ");
  8. @ Else @*/
  9. Document.Write("You can see this line of text in other browsers (such as Firefox, IE 4.x) <br>");
  10. /* @ End
  11. @*/
  12.  
  13. </Script>
  14.  
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_onStatement 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:

  • @ If
  • @ Elif
  • @ Else
  • @ End

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
  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 a 32-bit windows. The browser is IE. ");
  6. @ End
  7. @*/
  8.  

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
  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 browser is not IE (for example, Firefox) or IE in 32-bit windows. ");
  6. /* @ End
  7. @*/
  8.  

Familiar with annotation labels. In this exampleElseSome 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
  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. @*/
  10.  
If, elseif, else logic 2 (including 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. @*/
  10.  

Comprehensive processing. In the last exampleElseThe 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@ _ WinAnd@ 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. @*/
  8.  
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
  1. @ Set @ varname = term
  2.  

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
  1. @ Set @ myvar1 =35
  2. @ Set @ myvar3 = @ _ jscript_version
  3.  

Standard operators can be used in Conditional compilation logic:

  • ! ~
  • */%
  • +-
  • <>>>>
  • <=> =
  • =! ===! =
  • & ^ |
  • & |

You can determine whether to returnNaNTo determine whether a user-defined variable is defined:

Language: javascript, parsed in: 0.002 seconds, using GeSHi 1.0.7.12
  1. @If (@ NewVar! = @ NewVar)
  2. // The variable is not defined.
  3.  

BecauseNaNIs 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
  1. FunctionHttpRequest(Url, parameters){
  2. VarPageRequest =False // Variable to hold ajax object
  3. If (Window.XMLHttpRequest) // If Mozilla, Safari etc
  4. PageRequest =NewXMLHttpRequest()
  5. Else If (Window.ActiveXObject){ // If IE
  6. Try {
  7. PageRequest =NewActiveXObject("Msxml2.XMLHTTP")
  8. }
  9. Catch (E){
  10. Try{
  11. PageRequest =NewActiveXObject("Microsoft. XMLHTTP")
  12. }
  13. Catch (E){}
  14. }
  15. }
  16. Else
  17. Return False
  18. }
  19.  

Many people thinkTry/catchThe statement can successfully test Ajax support. Unfortunately, this is not true. UnsupportedThrow/catchBrowsers 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
  1. FunctionHttpRequest(Url, parameters){
  2. VarPageRequest =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.  
  19. If (! PageRequest &&TypeofXMLHttpRequest! ='Undefined')
  20. PageRequest =NewXMLHttpRequest()
  21. }
  22.  

Use Conditional compilation, completeTry/catchThe 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.)

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.