How to use js in html

coldplay.xixi
Release: 2021-03-04 14:18:03
Original
8889 people have browsed it

How to use js in HTML: 1. Use [<script>] to embed JavaScript in HTML, and use the src attribute when using [<script>] to include external files; 2. All [< script>] elements should be placed within the elements of the page.

How to use js in html

The operating environment of this tutorial: Windows 7 system, HTML version 4.01, DELL G3 computer.

How to use js in HTML:

1. The <script> element

is used in HTML<script>Embedded JavaScript

HTML 4.01 defines the following 6 attributes for

<script>.

  • async: Optional. Indicates that the script should be downloaded immediately, but should not prevent other operations on the page, such as downloading other resources or waiting for other scripts to be loaded. Only valid for external script files.

  • charset: Optional. The character set representing the code specified through the src attribute. This attribute is rarely used because most browsers ignore its value.

  • defer: Optional. Indicates that the script can be delayed until the document is fully parsed and displayed. Only valid for external script files. IE7 and earlier also support this attribute for embedded scripts.

  • language: Deprecated. Originally used to represent the scripting language used to write code (such as JavaScript, JavaScript1.2 or VBScript). Most browsers ignore this attribute, so there is no need to use it.

  • src: Optional. Represents an external file containing code to be executed.

  • type: Optional. Can be thought of as an alternative attribute to language; indicating the content type (also called a MIME type) of the scripting language used to write the code. Although both text/javascript and text/ecmascript have been deprecated, people have always used text/javascript. In fact, the

MIME type used by the server when transmitting JavaScript files is usually application/x–javascript, but setting this in type may cause the script to be ignored. In addition, the following values ​​can also be used in non-IE browsers:

application/javascript and application/ecmascript. Taking into account convention and maximum browser compatibility, the current value of the type attribute is still

text/javascript. However, this attribute is not required. If this attribute is not specified, its default value is still text/javascript.

There are two ways to use the <script> element: embedding JavaScript code directly in the page and including an external JavaScript file. When using the <script> element to embed JavaScript code, you only need to specify the type attribute for <script>. Then, just place the JavaScript code directly inside the element like this:

<script type="text/javascript">
function fun(){
    alert("hello")
}
</script>
Copy after login

The JavaScript code contained inside the <script> element will be interpreted from top to bottom.

No other content on the page will be loaded or displayed by the browser until the interpreter has evaluated all

code inside the <script> element.

When using <script> to embed JavaScript code, remember not to include the string anywhere in the code. If it must be present, use the / escape character <\/script>

Use the src attribute when including external files using <script>. Processing of the page is stopped while external files are parsed (including downloads). No other code can be embedded in the middle of the <script> with the src attribute, otherwise it will not be executed.

2. Tag position

According to traditional practice, all <script> elements should be placed within the elements of the page, such as:

Do this or the browser will start displaying the page after all js files have been downloaded, parsed and executed (the browser will not start rendering content until it encounters the body)

In order to avoid this problem, modern Web applications It is common to place all JavaScript quotes after the page content in the element, as shown in the following example:

<!DOCTYPE html>
<html>
<head>
<title>Example HTML Page</title>
</head>
<body>
<!-- 这里放内容 -->
<script type="text/javascript" src="example1.js"></script>
<script type="text/javascript" src="example2.js"></script>
</body>
</html>
Copy after login

In this way, the content of the page will be fully rendered before the included JavaScript code is parsed in the browser. Users will also feel that the speed of opening the page is accelerated because the time the browser window displays a blank page is shortened.

3. Document mode

doctype

Mixed mode

Standard mode

Quasi-standard mode

4.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template