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 .......