js002-using JavaScript in HTML

Source: Internet
Author: User

js002- in the HTML used in JavaScript

2.1 <script> Elements

The following 6 Properties are defined

Async: Optional. Indicates that the script should be downloaded immediately, but does not interfere with other actions on the page, such as downloading additional resources or waiting for other scripts to load. Valid only for external script files. (asynchronous)

CharSet: Optional. Represents the character set of the code specified by the SRC attribute.

Defer: Optional. Indicates that the script can be deferred until the document is fully parsed and displayed before execution, only valid for external scripts. This property is also supported by IE7 and its earlier versions. (Delayed, postponed)

SRC: Optional. Represents the external file that contains the code to execute.

Type: Optional. Can be seen as an alternative attribute of language, representing the same type of scripting language used in writing code

<script type= "Text/javascript" ></script>

There are two ways to use the <script> element: embed JavaScript code directly and include external JavaScript files

2.1.1 embed JavaScript code directly:

<script type= "Text/javascript" >

function Sayhi () {

Alert ("hi~");

}

</script>

When embedding JavaScript code directly, do not appear in the code </SCRIPT>, otherwise the browser will think that is an end tag, through the escape character can solve the problem

There is a problem:

<script type= "Text/javascript" >

function Sayhi () {

alert ("</script>");

}

</script>

Solve the problem:

<script type= "Text/javascript" >

function Sayhi () {

alert ("<\/script>");

}

</script>

2.1.2 Introducing external JavaScript files

Must contain the SRC attribute, which is a link to an external JavaScript file.

<script type= "Text/javascript" src= "Example.js" >

function Sayhi () {

Alert ("hi~");

}

</script>

2.1.3 the location of the label

Usually placed in the

<! DOCTYPE html>

<title>example page</title>

<script type= "Text/javascript" src= "Example1.js" ></script>

<script type= "Text/javascript" src= "Example2.js" ></script>

<body>

Content

</body>

Put it behind all the content so the browser shows the entire page before parsing the JavaScript code, which speeds up the opening.

<! DOCTYPE html>

<title>example page</title>

<body>

Content

<script type= "Text/javascript" src= "Example1.js" ></script>

<script type= "Text/javascript" src= "Example2.js" ></script>

</body>

2.1.4 Delay Script defer Properties (for external files only)

Using the defer attribute in the <script> element is equivalent to telling the browser to download it immediately, single-deferred execution. ( not executed until element is encountered )

<! DOCTYPE html>

<title>example page</title>

<script type= "Text/javascript" defer= "defer" src= "Example1.js" ></script>

<script type= "Text/javascript" defer= "defer" src= "Example2.js" ></script>

<body>

Content

</body>

In the HTML document, set the Defer property to Defer= "defer".

2.1.5 Asynchronous Script Async property to change the behavior of the processing script, only for external scripts

<! DOCTYPE html>

<title>example page</title>

<script type= "Text/javascript" async= "async" src= "Example1.js" ></script>

<script type= "Text/javascript" async= "async" src= "Example2.js" ></script>

<body>

</body>

The second script may be executed before the first script, so it is important to ensure that the two scripts are not related to one another.

In the HTML document, set the Sync property to async= "Async"

The use of 2.1.6 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>

Use (&lt;) Less than sign (<) in the replacement code

such as: if (a &lt; b)

2.2 embed code with external files

It is best to apply external files, external files have the following advantages:

Maintainability: Easy to maintain code

Cacheable: All external JavaScript files that the browser can cache to connect based on the specific settings

Adapting to the future:

2.3 Document Mode

promiscuous mode and Standard mode

2.4 <noscript> Elements

shown in the following set of conditions (usually in body)

Browser does not support scripting

Browsers support scripts, but scripts are disabled

2.5  

js002-using JavaScript in HTML

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.