Several small JavaScript Cases

Source: Internet
Author: User

Javascript is simple and easy to use, but sometimes it is confused by some small cases. Although it has similar syntaxes with mainstream languages, it has many features. To understand these small features, you will feel that it is very powerful, but it will go to another extreme.

 

1. Script tag

Do not mark the content in the <SCRIPT> tag </SCRIPT>. html parses the content into an end tag and generates an error.

 
1<SCRIPT type ="Text/JavaScript">
2Alert ("</SCRIPT>");
3</SCRIPT>

An error will occur. We can write: Alert ("</SC" + "ript> ");

2. Defer delayed execution

1<SCRIPT src = "scripts/demo. js" type = "text/JavaScript" Defer = "Defer"> </SCRIPT>

It is used only for external JS and identifies the JS that the Browser executes after parsing the complete page.

 

However, we recommend that you put JS at the bottom of the page to execute JS after page parsing, instead of using defer.

 

Not all browsers support defer, but currently mainstream Browser IE, ff, and chrome support defer.

3. Why? // <! [CDATA [ 

We should have seen the following recommended JS statements, but we can run normally without adding them ??

1<SCRIPT type = "text/JavaScript">
2//<! [CDATA [
3//Your JS Code
4//]>
5</SCRIPT>

 

Why do you want to write it like this?Code

1<SCRIPT type = "text/JavaScript">
2If(A <B)
3{
4//Your code
5}
6</SCRIPT>

 

This code is okay in HTML, but in the XHTML standard, all <will be parsed as a tag. If it is followed by spaces, the parsing syntax will be incorrect, the preceding syntax is incorrect in XHTML.

For compatibility, we can use & lt; instead, and this can run normally, but this code will be hard to understand and is not recommended.

Method 2:

1<SCRIPT type = "text/JavaScript">
2<! [CDATA [
3If(A <B)
4{
5//Your code
6}
7]>
8</SCRIPT>

 

In XHTML, CDATA fragment code is treated as common text.

However, some browsers that are not compatible with XHTML do not recognize CDATA. Therefore, the above recommended statement is displayed.

 

PS: currently, mainstream browsers do not need to add CDATA for normal resolution. It may be that the browser has performed special processing.


4. Add <NoScript> </NoScript> to your page.

This line of code is still necessary for more and more platforms and devices to connect to the Internet and more diverse users.

 

5. assign values directly to variables that have never been declared

Assign a value to a variable that has never been declared without generating an error. Instead, a global variable is generated, which is released only when the page is closed.

PS: Compared with the server-side language (C #, Java, etc.), JavaScript runs in users' browsers, so you should pay more attention to managing your own variables, releasing andProgramThe logic, user machine configuration, and browser version are unpredictable. Misuse of user resources and unexpected results should be avoided.

 

Not complete .......

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.