JavaScript Advanced Programming (i)

Source: Internet
Author: User
Tags cdata

The 2nd chapter in the book, using JavaScript summary summary in HTML

2.1 <script> Elements

5 Properties in <script>:
CharSet: Optional. Represents the character set of the code specified by the SRC attribute. Most browsers ignore its value and few people use it.
Defer: Optional. Indicates that the script can be deferred until the document is fully parsed and displayed before execution. IE and Firefox3.1 are currently the only mainstream browsers that support defer properties. Other browsers are ignored and do not delay the execution of the script.
Language: Deprecated. Originally used to represent scripting languages (such as JavaScript, JavaScript1.2, or VBScript) used to write code.
SRC: Optional. Represents the external file that contains the code to execute.
Type: Required. Represents the content type (also known as MIME type) of the scripting language in which the code is written. It is usually text/javascript.

The JavaScript code contained within the <script> element will be interpreted from top to bottom. The rest of the page will not be loaded or displayed by the browser until all the code within the <script> element is evaluated by the interpreter.

When you use <script> embed JavaScript code, the "<script>" string does not appear anywhere.
<script type= "Text/javascript" >
function Sayscript () {
Alert ("</script>"); Here will be an error
}
</script>
"</script>" can be written separately to avoid errors.
<script type= "Text/javascript" >
function Sayscript () {
Alert ("</SCR" + "ipt>");
}
</script>

The SRC attribute can point to a URL in a domain outside the domain of the current HTML page, such as
<script type= "Text/javascript" src= "Http://www.somewhere.com/afile.js" ></script>

2.1.1 The location of the label

As a rule, all <script> elements should be placed in the <title>example HTML page</title>
<body>
<!--content here--
<script type= "Text/javascript" src= "Example1.js" ></script>
<script type= "Text/javascript" src= "Example2.js" ></script>
</body>

The use of 2.1.3 in XHTML

Some JavaScript code is valid in HTML, but it is not valid in XHTML:
<script type= "Text/javascript" >
function Compare (A, b) {
if (a < b) {
Alert ("A is less than B");
} else if (a > B) {
Alert ("A is greater than B");
} else {
Alert ("A is equal to B");
}
}
</script>
The less than sign (<) in the comparison statement a < b in the code will be interpreted as starting a new label in XHTML. However, as a label, the less-than-sign cannot be followed by a space, resulting in a syntax error.
There are two ways to avoid similar grammatical errors. The first is to replace the less-than sign (<) with the corresponding HTML entity (&lt;), and replace the following code:
<script type= "Text/javascript" >
function Compare (A, b) {
if (a &lt; b) {
Alert ("A is less than B");
} else if (a > B) {
Alert ("A is greater than B");
} else {
Alert ("A is equal to B");
}
}
</script>
This can be run, but not easy to understand. Therefore, the second method can be used to include JavaScript code with a CDATA fragment. In XHTML (XML), CDATA fragments are a special area in a document that can contain text content in any format that you do not need to parse.
<script type= "Text/javascript" >
Some browsers are incompatible with XHTML and therefore do not support CDATA fragments, and you can use JavaScript annotations to comment out CDATA tags
<! [cdata[
function Compare (A, b) {
if (a < b) {
Alert ("A is less than B");
} else if (a > B) {
Alert ("A is greater than B");
} else {
Alert ("A is equal to B");
}
}
]]>
</script>

2.3 <noscript> Elements

This element can contain any HTML elements that can appear in document <body> except for the--<script> element, and the content contained in the <nosript> element is displayed only in the following cases:
1. The browser does not support scripting
2. The browser supports scripts, but scripts are disabled.
<title>example HTML page</title>
<script type= "Text/javascript" src= "Example1.js" ></script>
<script type= "Text/javascript" src= "Example2.js" ></script>
<body>
<noscript>
<p> This page requires browser support (enabled) javascript</p>
</noscript>
</body>

All of the above is from the book "JavaScript Advanced Programming (2nd edition)" [US] Nicholas c.zakas Lisongfeng Tianhua Translation

JavaScript Advanced Programming (i)

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.