Home Web Front-end HTML Tutorial How to use js in html

How to use js in html

Mar 04, 2021 pm 02:18 PM
html javascript

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles