Home Web Front-end JS Tutorial What methods can be used to embed js in html documents?

What methods can be used to embed js in html documents?

May 23, 2018 pm 01:54 PM
html javascript document

This time I will bring to you what methods can be used to embed js in html documents, what are the precautions for embedding js in html documents, the following is a practical case, let’s take a look one time.

Embedding JavaScript in HTML

There are 4 ways to embed client-side JavaScript code in HTML documents:

1 .Inline, placed between the <script> and </script> tags (less);

2. Placed in an external file specified by the src attribute of the <script> tag (more) ;</p> <p style="text-align: left;">3. Place it in a self-<a href="http://www.php.cn/code/8922.html" target="_blank">HTML event handler</a>, which is specified by an HTML attribute value such as onclick or <a href="http://www.php.cn/wiki/1457.html" target="_blank">onmouseover</a> (rarely);</p> <p style="text-align: left;">4. Put it in a URL. This URL uses the special protocol "javascript" protocol (rarely); </p> <p style="text-align: left;"><span style="color: #0000ff">0——Attachment: Script type</span></p> <p style="text-align: left;">JavaScript is the original scripting language of the Web. By default, the <script> element contains or references JavaScript code. If you want to use a non-standard scripting language, such as VBScript, you must use the type attribute to specify the MIME type of the script, for example: </p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;script type=&quot;text/vbscript&quot;&gt;   ... ...  &lt;/script&gt;</pre><div class="contentsignin">Copy after login</div></div> <p style="text-align: left;"><span style="color: #ff0000">The default value of the type attribute is "text/javascript". </span></p> <p style="text-align: left;"><span style="color: #0000ff">1——Inline <script> element</span></p> <p style="text-align: left;">For example: </p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"> &lt;script&gt;   function displayTime(){   ... ...   }   window.onload = displayTime;  &lt;/script&gt;</pre><div class="contentsignin">Copy after login</div></div> <p style="text-align: left;"><span style="color: #0000ff">2——Src attribute uses external The script</span></p> <p style="text-align: left;"><script> tag in the file supports the src attribute, which specifies the URL of the file containing JavaScript code. Its usage is as follows: </p> <p style="text-align: left;"><span style="text-decoration:underline;">Copy code</span> The code is as follows:</p> <p style="text-align: left;"><script src="../../scripts/util.js">< /script></p> <p style="text-align: left;">When using the src attribute, anything between the <script></script> tags will be ignored.

When you include a script in a page using the src attribute, you give the script full control over the Web page.

3——Event handler in HTML

When the HTML file where the script is located is loaded into the browser, the JavaScript code in this script will only be executed once . JavaScript code can register event handlers by assigning functions to properties of the Element object. This Element object represents an HTML element in the document.

For example:

Copy code The code is as follows:

The attributes of the event handler defined in HTML can contain any JavaScript statements, separated by commas. These statements form the body of a function, which then becomes the value of the corresponding event handler property.

4 - JavaScript in the URL

Following the URL with a javascript: protocol qualifier is another way to embed JavaScript code into the client. This special protocol type specifies that the URL content is an arbitrary string, which is JavaScript code that will be run by the JavaScript interpreter. It is treated as a separate line of code, which means statements must be separated by semicolons and comments must be replaced by /**/ comments. The resource recognized by javascript:URL is the return value of the executed code converted into a string. If the code returns undefined, then the resource has no content.

javascript: The URL can be anywhere a regular URL can be used: such as the href attribute of the tag, the action attribute of

, or even window.open () method parameters.

The JavaScript URL in the hyperlink can be like this:

<a href="javascript:new Date().toLocaleTimeString();" rel="external nofollow" > 
What time is it? 
</a>
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps to use Promise in ES6

How to use React Form to complete component encapsulation

The above is the detailed content of What methods can be used to embed js in html documents?. For more information, please follow other related articles on the PHP Chinese website!

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
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.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

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