JavaScript in HTML
The primary method of inserting JavaScript into an HTML page is via the <script> element.
The type attribute of the <script> element indicates the content type (also called MIME type) of the scripting language being used by the code block. The type attribute is safe to omit, as text/javascript is it’s default value.
The String "</script>"
When using inline JavaScript code, keep in mind that you cannot have the string <script> anywhere in your code. For example, the following code causes an error when loaded into a browser:
|
|

Because of the way that inline scripts are parsed, the browser sees the string </script> as if it were the closing tag. This problem can be avoided easily by escaping the / character, as in this example:
|
|
The src attribute and inline code can not be used at the same time
It’s important to note that a <script> element using the src attribute should not include additional JavaScript code between the <script> and </script> tags. If both are provided, the script file is downloaded and executed while the inline code is ignored.
Include JavaScript at the end of <body>
Using this approach, the page is completely rendered in the browser before the JavaScript code is processed. The resulting user experience is perceived as faster, because the amount of time spent on a blank browser window is reduced.
|
|
The <noscript> element
The <noscript> element can contain any HTML elements, aside from <script>, that can be included in the document <body>. Any content contained in a <noscript> element will be displayed under only the following two situations:
- The browser doesn’t support scripting.
- The browser’s scripting support is turned off.
|
|
References:
Nicholas C. Zakas ,JavaScript高级程序设计(第3版):人民邮电出版社 ,2012-3-29 ,第二章