Conditional compilation of JScript/JavaScript in IE

Source: Internet
Author: User
Tags comment tag

In IE, there is a little known feature called Conditional compilation. supported since ie4, this feature starting getting some attention when it began showing up in some Ajax related javascripts. an absolute form of object detection, Conditional compilation lets you dictate to IE whether to compile certain parts of your JScript or JavaScript code depending on predefined and user defined conditions. think of it as conditional comments for your script that can also be molded to work gracefully with non ie browsers as well.

Syntax Overview

Conditional compilation is activated by using@ Cc_onStatement inside your script, or by directly using@ IfOr@ SetStatement that are part of the logic of CC. Here's an alternative strative example:

<SCRIPT type = "text/JavaScript">/* @ cc_ondocument.write ("jscript version:" + @ _ jscript_version + ". <br> ");/* @ if (@ _ jscript_version> = 5) document. write ("jscript version 5.0 or better. <br \/> "); document. write ("this text is only seen by browsers that support JScript 5 + <br>"); @ else @ */document. write ("this text is seen by all other browsers (ie: Firefox, ie 4.x etc) <br>");/* @ end @ */</SCRIPT>

Example:

This text is seen by all other browsers (ie: Firefox, ie 4.x etc)

If you're using IE (of any version), you should see the first document. write () rendered, and for ie5 +, the following two document. write () as well (since JScript 5 is supported by ie5 + ). the last document. write () method is served only to non ie5 + browsers, whether it's Firefox, opera, ie4, you name it. conditional compilation relies on tag teaming with the comment tag, similar to in conditional comments, to ensure it works harmoniously in all browsers.

When working with Conditional compilation, it's best to first activate it via@ Cc_onStatement,As only then can you also include comment tags in your script in a way that ensures browser compatibility, As shown in the example above.

@ If, @ Elif, @ else, And @ end statements

So with the formalities out of the way, here are the conditional statements at your disposal for Conditional compilation:

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

Lets see some "eccentric" Examples now.

If else logic (ie exclusive)
 
/* @ 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 @*/

Here the entire script is only rendered by IE browsers and ignored by all else, and depending on the bit of your OS, a different message is shown. Contrast that with the next example...

If else Logic II (other browsers random SIVE)
 
/* @ Cc_on/* @ if (@ _ Win32) document. write ("OS is 32-bit, browser is IE. "); @ else @ */document. write ("browser is not IE (ie: Is Firefox) or browser is not 32 bit IE. ");/* @ end @*/

By manipulating the comment tag, the "else" part in this example will get picked up by all non ie browsers such as Firefox, plus non 32 bit ie as well. study the comments until your head spins, and you'll see the logic.

If, elseif, else logic (ie exclusive)

Moving on, time for the full monty:

 
/* @ 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 II (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 @*/

Sane deal here. In this 2nd example of the 2nd set, the final "else" Statement gets picked up by non ie browsers.

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.