Basic application methods of javascript embedded and external links

Basic application methods of javascript embedded and external links

Basic application of javascript embedded and external links

The js language can be completely placed in the html language

But depending on the situation, it can be divided into embedded and external links.

When there is less js code, you can directly use the embedded method to introduce javascript through the <script></script> tag and related attributes

<!DOCTYPE html>
<html>
<script>
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
</script>
<body>
 
</body>
</html>

However, in large projects and when there are a lot of javascript codes, you need to use external links.

When using external links, you need to create a new .js file first

<!DOCTYPE html>
<html>
<script type="text/javascript" src="1245.js">
</script>
<body>
 
</body>
</html>

Put the javascript code you need to write in the 1245.js file

Three ways to write JavaScript: inline, embedded, and externally linked

1. Inline

In the attributes of the HTML tag, the execution is triggered by the behavior. (Rarely used)

insert image description here

2. Embedded

Wrap it in the HTML file using a script tag and it will execute automatically when the page is opened. (It’s easy to use, but don’t use it with large amounts of JS code.)

insert image description here

3. External link

Written in a separate js file, introduced in the html file using the src attribute of the script tag, it will be automatically executed when the page is opened. (Recommended because there are various optimization methods)

insert image description here

insert image description here

Note : It is rarely used in the industry, almost never used. Inline and external links cannot share the same script tag. In principle, the script tag of the external link can be placed anywhere. The script attribute of the external link is src , not href, not href, not href...

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • JavaScript dynamically adds styles (inline/embedded/external link rules, etc.)
  • A simple way to get the external link style of an element in js
  • How to get the element's external link style in js

<<:  What is flex and a detailed tutorial on flex layout syntax

>>:  Two ways to solve the problem of MySQL master-slave database not being synchronized

Recommend

MySql 8.0.11-Winxp64 (free installation version) configuration tutorial

1. Unzip the zip package to the installation dire...

MySQL 8.0.16 winx64 installation and configuration method graphic tutorial

I just started learning about databases recently....

Detailed Example of JavaScript Array Methods

Table of contents Introduction Creating an Array ...

Illustration of the process of using FileZilla to connect to the FTP server

When I first started setting up an ftp server on ...

CentOS7 deploys version 19 of docker (simple, you can follow it)

1. Install dependency packages [root@localhost ~]...

Analysis of MySQL general query log and slow query log

The logs in MySQL include: error log, binary log,...

A brief discussion on whether CSS will block page rendering

Maybe everyone knows that js execution will block...

Fixed a bug caused by scrollbar occupying space

background This bug was caused by滾動條占據空間. I check...

MySQL 8.0.22 decompression version installation tutorial (for beginners only)

Table of contents 1. Resource download 2. Unzip t...

Detailed explanation of how to use WeChat mini program map

This article example shares the specific implemen...

Introduction to MyCat, the database middleware

1. Mycat application scenarios Mycat has been dev...

Start a local Kubernetes environment using kind and Docker

introduce Have you ever spent a whole day trying ...

InnoDB type MySql restore table structure and data

Prerequisite: Save the .frm and .ibd files that n...

Vue.js implements timeline function

This article shares the specific code of Vue.js t...