The alert () function in JavaScript use the technique to explain _javascript techniques

Source: Internet
Author: User

In JavaScript code, you can use the alert () function of the Window object to display a piece of text, to debug your program, or to alert the user to the relevant information:


Copy Code code as follows:

Use Window object ' s alert () function
Window.alert ("sample text");


This can be simplified as a direct use of the alert () function:


Copy Code code as follows:

Simplified alert () usage
Alert ("Sample text");


If you need to display text with a newline, you can use \ n:


Copy Code code as follows:

Use \ n in alert ()
Alert ("The line\nthe second line");

If you need to use tab characters, you can use \ t:

Copy Code code as follows:

Use \ t in alert ()
Alert ("Alex\t50\t34\nbob\t59\t38");

Use of variables

In addition to displaying a static string, the alert () function can accept a variable and concatenate the value of the variable with another string:

Copy Code code as follows:

Use variable in alert ()
var word = "life";
Alert ("The magic word is: + Word +".) Don ' t panic. ");

Unfortunately, although the alert () function can accept a variable, it only ends with this string concatenation operation; the alert () function does not accept the practice of passing arguments to a string, as opposed to another debugging method, Console.log (). Take the following code as an example:

Copy Code code as follows:

Try to use parameter in alert (), would fail
var name = "Bob";
var years = 42;
Alert ("%s is%d years old.", name, years);


If the alert () function takes a string pass, the expected output will be "Bob is years old." But the alert () function does not support this, so the final output is '%s is '%d years old. '

pop-up window style

Because the pop-up box used by the alert () function is a browser system object rather than a Web page document object, the style-html label for the pop-up box cannot be defined by using an HTML tag in the alert () function to be displayed intact. For the following code:


Copy Code code as follows:

Try to use HTML tags in alert (), would fail
Alert ("<b>test text</b>");

The output is not bold "Test Text."

If you do need to change the style of the alert box, there are two options:

1. Use Unicode characters in the alert () function. The benefits of this approach are simple to implement, but their limitations are obvious: Unicode characters are very limited in their expressiveness.

2. Instead of using the alert () function, use the HTML component to simulate a pop-up box (for example, using the jquery UI Dialog). The advantage of this scheme is that the performance of the pop-up box is strong, but its use will increase the complexity of the front-end code.

Conclusion

The alert () function can be used to alert a user to information or to debug a program. For the former, using components such as the jquery UI dialog can dramatically increase the expressiveness and user experience, and for the latter, because the alert () pop-up blocks the execution of JavaScript code, in many cases using Console.log () To debug a program is a better solution.

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.