JavaScript Getting Started: Variables and try catch ()

Source: Internet
Author: User
Tags try catch

E has a little-known function called conditional compilation (conditional compilation). Since the IE4 began to support conditional compilation, this feature has gradually gained more attention and is used in its own JavaScript code, even in some Ajax code will often find its figure. Conditional compilation allows you to control whether IE browser executes your custom script section, but does not affect other browsers, and other browsers will only treat these custom scripts as normal script annotations.

Syntax overview
Enable conditional Compilation of keywords/ * @cc_on, ending with @*/. You can also use logical statements such as @if or @set. The following is a simple example:

<script type= "Text/javascript" >
/* @cc_on alert ("JScript version:" + @_jscript_version);
/ * @if (@_jscript_version >= 5) alert ("This sentence is only available to a browser with a JScript version of 5.0 or higher"); @else @*/
Alert ("This sentence can be seen by other browsers (Firefox, Opera, IE 4.x etc)");
/ * @end @*/</script>


If you use IE browser (any version), you will see that the first alert content is "JScript version: XX"
If you use IE5 and above browsers, you will see the second alert content is "JScript version 5.0 or higher".
If you use other browsers, such as Firefox, Opera,chrome, you'll only see a third alert.
Use conditional statements: @if, @elif, @else, @end
To attach a statement that declares a condition within a conditional compilation:

@if
@elif equivalent to ElseIf in other grammars
@else
@end
Let's look at a few examples of "odd":

If else's logical ⅰ (ie only)
/ * @cc_on @if (@_win32) alert (operating system is 32-bit. Browser is IE. "); @else alert (the operating system is not 32-bit. Browser is IE. "); @end @*/


Determine if the operating system is 32 bits, and the entire script executes only under IE, ignoring all other browsers. Next, let's look at the difference between the above example and the following example:

Logical Ⅱ of If else (also applicable to other browsers)
/ * @cc_on @if (@_win32) alert (operating system is 32-bit. Browser is IE. "); @else @*/Alert ("Browser is not IE or operating system is not 32 bits"); / * @end @*/


By manipulating the position of the annotation tag, let the above example support the judgment of non ie browsers. The second alert will be performed by all not 32-bit systems or non IE browsers. You will find the secret of annotation logic only when the study is too big:

If,elseif,else logical Ⅰ (for IE only)
/ * @cc_on @if (@_jscript_version >= 5) alert ("Current IE browser supports JScript 5+"); @elif (@_jscript_version >= 4) alert (" Current IE browsers support JScript 4+ "); @else alert ("Is your ie a relic?"). "); @end @*/


If,elseif,else logical Ⅱ (also applicable to other browsers)
/ * @cc_on / * @if (@_jscript_version >= 5) alert ("Current IE browser supports JScript 5+"); @elif (@_jscript_version >= 4) alert ("Current IE browser support 4+"); @else @*/Alert ("Non-IE browser (a browser that does not support JScript)"); / * @end @*/


In the last example, notice how the last else is written, and not IE browsers are involved in the judgment.


JS variable


Syntax overview
Enable conditional Compilation of keywords
/ * @cc_on, ending with @*/. You can also use logical statements such as @if or @set. The following is a simple example:

<script type= "Text/javascript" > /* @cc_on alert ("JScript version:" + @_jscript_version); / * @if (@_jscript_version >= 5) alert ("This sentence is only available in JScript version 5.0 or higher"); @else @*/alert ("This phrase can be seen by other browsers (Firefox, Opera, IE 4.x etc) "); / * @end @*/</script>


If you use IE browser (any version), you will see that the first alert content is "JScript version: XX"
If you use IE5 and above browsers, you will see the second alert content is "JScript version 5.0 or higher".
If you use other browsers, such as Firefox, Opera,chrome, you'll only see a third alert.
Use conditional statements: @if, @elif, @else, @end
To attach a statement that declares a condition within a conditional compilation:

@if
@elif equivalent to ElseIf in other grammars
@else
@end
Let's look at a few examples of "odd":

If else's logical ⅰ (ie only)
/ * @cc_on @if (@_win32) alert (operating system is 32-bit. Browser is IE. "); @else alert (the operating system is not 32-bit. Browser is IE. "); @end @*/


Determine if the operating system is 32 bits, and the entire script executes only under IE, ignoring all other browsers. Next, let's look at the difference between the above example and the following example:

Logical Ⅱ of If else (also applicable to other browsers)
/ * @cc_on @if (@_win32) alert (operating system is 32-bit. Browser is IE. "); @else @*/Alert ("Browser is not IE or operating system is not 32 bits"); / * @end @*/


By manipulating the position of the annotation tag, let the above example support the judgment of non ie browsers. The second alert will be performed by all not 32-bit systems or non IE browsers. You will find the secret of annotation logic only when the study is too big:

If,elseif,else logical Ⅰ (for IE only)
/ * @cc_on @if (@_jscript_version >= 5) alert ("Current IE browser supports JScript 5+"); @elif (@_jscript_version >= 4) alert (" Current IE browsers support JScript 4+ "); @else alert ("Is your ie a relic?"). "); @end @*/


If,elseif,else logical Ⅱ (also applicable to other browsers)
/ * @cc_on / * @if (@_jscript_version >= 5) alert ("Current IE browser supports JScript 5+"); @elif (@_jscript_version >= 4) alert ("Current IE browser support 4+"); @else @*/Alert ("Non-IE browser (a browser that does not support JScript)"); / * @end @*/


In the last example, notice how the last else is written, and this non-IE browser also participates in the judgment.


Try catch ()


at the beginning of the first section, I have mentioned conditional compilation being used more and more in Ajax. Now I'm going to explain why. Different browsers provide asynchronous requests with different objects, resulting in a perfect Ajax program that must first determine the type and version of the current browser in the core function.

A typical example of an AJAX code can help you understand these:

function HttpRequest (URL, parameters) {
var pagerequest = false// Defines a variable that obtains an Ajax object and assigns an initial value (good habit) if (window). XMLHttpRequest//Mozilla systems such as Firefox, WebKit, Safari and Chrome, and opera support this object
Pagerequest = new XMLHttpRequest () else if ( Window. ActiveXObject) {//only IE support, and more than one try {pagerequest = new ActiveXObject ("Msxml2.xmlhttp")} catch (e) {try{= n EW ActiveXObject ("Microsoft.XMLHTTP")} catch (e) {}} else return false}
Most people think the script above can be executed successfully under IE. But unfortunately, that is not the case. Some browsers do not support "Throw/catch" browsers, such as the evil IE series of small four (ie 4.x). These browsers that do not support throwing the fetch will break and return an error before being thrown. To address this flaw, we use conditional compilation to rewrite the above code into an AJAX function that truly implements a reasonable Cross-browser operation:

An AJAX function that really implements a reasonable cross-browser operation

function HttpRequest (URL, parameters) {
var pagerequest = false//define variable to get Ajax object and assign initial value (good habit)
/* @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 ()}
Because only the IE5 and above browsers use try/catch that part of the judgment, so use conditional compilation to distinguish it from the normal judgment area. The reality is that you rarely encounter IE4 or other similar browsers. Firefox and other browsers that support the standard will obviously use XMLHttpRequest. So you get an AJAX function that really implements a reasonable cross-browser run!

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.