JavaScript language Primer: Events, cookies, timing, etc.

Source: Internet
Author: User
Keywords Cookie javascript Language introduction

This article, as part 2nd of the series, continues to explain the basics of http://www.aliyun.com/zixun/aggregation/33906.html ">javascript, including events, Try...catch statements , the use of cookies and timing functions.

The first "Start with JavaScript language" article covers many of the most basic elements of the JavaScript language, from creating script tags to using annotations, including JavaScript files in HTML documents, defining variables, using operators, defining arrays, using conditional statements, Define functions and use loops, and so on. Beginning with the end of the previous article, this article explains some of the other basic JavaScript language concepts and continues to provide beginners with a basic understanding of the language. The basics of this article will give you a better understanding of the library you are using, knowing how to make a decision about whether to use the library, or even giving you the courage to write your own library. There is a code example that completes all of these from beginning to end.

Event

Events are catalysts for adding any type of interactivity to a Web page using the JavaScript language. Each HTML element triggers a related event that is available to trigger JavaScript code. For example, an input field has many possible events: when someone clicks or goes to an input field, you can associate the focus event to trigger JavaScript code, or you can associate a blur when someone turns out or clicks a targeted input field Event to trigger the JavaScript code. The possibilities are endless when you have associated good events. For example, the Blur event can trigger JavaScript code to check if the input field has the correct data, and if not, display a message online as an automatic feedback. The following code provides an example of a focus and blur event that can be used to display default text in one input field:

<input type= "text" name= "email" value= "Enter your email address" onfocus= "this.value ="; "onblur=" if (this.value = = ') This.value = ' Enter your email address ';/>

Here is a field with a default value, and when viewed in a Web browser, the input field displays the text "Enter your email address." When someone clicks or goes to this field, in order for this default text to disappear, use the focus event and set a field value for an empty string. If someone turns out or clicks outside of the input field, use the Blur event to display the default text again, otherwise, leave their custom text.

Each HTML element has a time associated with it. Table 1 lists some of the most common HTML elements and the events associated with them.

Table 1. Common elements and their associated events

element Event body onload, onunload input onfocus, onblur, onchange, onkeydown, onkeypress, onkeyup form onsubmit img onmouseover , onmouseout, onclick

Try...catch and throw

The Try...catch statement provides a way to test your code without sending an error to the browser or displaying a custom error. If a JavaScript error is not within the Try...catch statement, no JavaScript code in progress is executed, and the browser retains its original form of processing and displaying the error. Use the try part of the statement to execute the JAVASCRIPT code; This structure can be used when executing code that may not work in some browsers. If the code is within the Try...catch statement, it is simply not executed. This error can be a real error message, or it may not do anything, based on whether the user needs to know that it appears.

Handling Errors with Try...catch

The catch part of the statement also includes a default parameter for an Error object. This Error object returns the relevant error message that occurred in the part of the try statement. This error object has two attributes: Message and line. The message provides the text of the exact error that the statement occurred, and line provides the exact lines of the code where the error occurred. Listing 1 shows an example of a Try...catch statement that uses the wrong object to warn the message and line. Of course, this information is useful only for the environment you are debugging, but these properties can be useful when trying to provide feedback to the user without relying on the browser to handle the error.

Listing 1. Use that Error object in the Try...catch statement to debug the error

try {//attempt to execute code that produces a error here} catch (err) {var txt = err.message + ' \ n '; txt = err.line; aler T (TXT); }

Handling error exceptions with throw statements

The TRY...CATCH structure provides a powerful handling error, but you can make it go further by using the throw statement. Throw statements allow you to create error exceptions based on certain situations, which provides a good opportunity to create friendlier user error messages in accurate and shallow readable languages. Listing 2 shows how to use the throw statement to create an example of an error exception that is based on the situation in the try part of the Try...catch statement.

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.