Basic knowledge about events, cookies, and timing in JavaScript

Source: Internet
Author: User
This article starts from the end of the previous article and explains some other basic JavaScript language concepts, continue to provide beginners with a basic understanding of the language. An article on "getting started with JavaScript language" covers many of the most basic content in 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, defining functions, and using loops. This article starts from the end of the previous article, explains some other basic JavaScript language concepts, and continues to provide beginners with a basic understanding of the language. The basic content mentioned in this article allows you to better understand the database you are using and decide whether to use the database or not, it may even give you the courage to write your own database. The document provides examples from beginning to end to illustrate how the language is implemented.

Event

An event is a catalyst that uses JavaScript to add any type of interaction to a webpage. Each HTML element has an event that can be used to trigger JavaScript code. For example, there are many possible events in the input field: You can associate the focus event to trigger JavaScript code when someone clicks or jumps to the input field, alternatively, you can associate a blur (defocus) event to trigger JavaScript code when someone clicks the outside of an input field that has received the focus or jumps out of the field. After an event is associated, there will be infinite possibilities. For example, the blur event triggers JavaScript code that checks whether the data in the input field is valid. If it is invalid, an inline message is displayed as an automatic feedback. The following code provides an example to illustrate how the focus and blur events are used to display the default text in the input field.

Onfocus = "this. value =''; "onblur =" if (this. value = '')
This. value = 'enter your email address'; "/>
The input field has a default value. Therefore, when you view the field in a Web browser, the input field displays the text "Enter your email address ". When someone clicks or jumps to the domain, the focus event is used to set the Domain value to an empty string to make the default value disappear. If someone clicks or jumps to the external field of the input field, the blur event is used to display the default text again. If you do not do this, the text they entered is left behind.

Each HTML element has its own related events. Table 1 lists some of the most common HTML elements and related events.

Table 1. common elements and related events

Element event

Body onload, onunload

Input onfocus, onblur, onchange, onkeydown, onkeypress, onkeyup

Form onsubmit

Img onmouseover, onmouseout, onclick

Try... catch and throw

Try... catch statements provide a way to detect code errors, so that you do not need to send errors to the browser or give a custom error. If a JavaScript error is not included in a try... if the catch statement is internal, no subsequent JavaScript code can be executed, and the browser has to handle and display the error in its own way. The try part of this statement is used to execute JavaScript code, while the catch part is used to handle errors that may occur in the try part. You can use this statement to construct code that can work normally in Some browsers. If the code is placed inside the try... catch statement, it is ignored and not executed when an error is returned. The catch part will handle the error. This error may indeed give an error message or do nothing, depending on whether the user needs to know the error.

Use try... catch to handle errors

The catch part of the statement can also contain a default error object parameter, which returns information related to the error in the try part of the statement. The error object has two attributes: message and line. The text provided by message describes the exact error, and line provides the exact number of lines of code for the error. Listing 1 provides an example of a try... catch statement. In this example, an error object is used to notify the error message and the number of rows. Of course, this kind of information is useful only in the debugging status, but it is intended to provide users with feedback about an error without relying on browser processing, these attributes become very useful.

Listing 1. Using Error objects in try... catch statements to adjust errors

The Code is as follows:


Try
{
// Try to execute the code that produces the error here
}
Catch (err)
{
Var txt = err. message + '\ n ';
Txt + = err. line;
Alert (txt );
}


An error occurs when the throw statement is used to create an error.

Try... catch constructs provide excellent error handling functions, but you can also take a further approach, that is, using throw statements. Throw statements allow you to create error exceptions based on certain conditions. This method provides the best opportunity to create more user-friendly error information, these error messages are accurate and described in easy-to-understand languages. Listing 2 provides a simple example to illustrate how to use the throw statement to create a condition-based error exception in the try part of the try... catch statement.

Listing 2. Using throw statements to create error exceptions

The Code is as follows:


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.