search
HomeWeb Front-endHTML TutorialHow to use js in html

How to use js in html

Mar 04, 2021 pm 02:18 PM
htmljavascript

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

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 </script>

is used in HTML<script></script>Embedded JavaScript

HTML 4.01 defines the following 6 attributes for

<script></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></script> element: embedding JavaScript code directly in the page and including an external JavaScript file. When using the <script></script> element to embed JavaScript code, you only need to specify the type attribute for <script></script>. Then, just place the JavaScript code directly inside the element like this:

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

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

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

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

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

2. Tag position

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

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>

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.

The above is the detailed content of How to use js in html. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Is HTML easy to learn for beginners?Is HTML easy to learn for beginners?Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft