2 methods and precautions for adding scripts in HTML

2 methods and precautions for adding scripts in HTML
How to add <script> script in HTML:

1. You can directly add javascript code to html

Copy code
The code is as follows:

<script type="text/javascript">
//javascript code
</script>

When the interpreter embeds <script> code, the processing of the HTML page will also be temporarily stopped.

2. Add external js files

Copy code
The code is as follows:

<script type="text/javascript" src="Address of external .js code"></script>

Note:
① In XHTML documents, the </script> tag can be omitted, for example:

Copy code
The code is as follows:

<script type="text/javascript" src="example.js" />

However, the </script> tag cannot be omitted in HTML. This tag does not comply with HTML specifications and therefore cannot be correctly parsed by some browsers.

② It is best to put the <script> tag before the </body> tag, which will enable the browser to load the page faster.

③When using <script> to embed JavaScript code, do not allow the “</script>” string to appear anywhere in the code. For example, a browser will generate an error when loading the following code:

Copy code
The code is as follows:

<script type="text/javascript">
function function1(){
alert("</script>");
}

Because according to the rules for parsing embedded code, when the browser encounters the string "</script>", it will think that it is the closing </script> tag. If you want to add a </script> string to the code, you can write it as <\/script>

<<:  Two ways to build a private GitLab using Docker

>>:  How to create a simple column chart using Flex layout in css

Recommend

js to call the network camera and handle common errors

Recently, due to business reasons, I need to acce...

Mycli is a must-have tool for MySQL command line enthusiasts

mycli MyCLI is a command line interface for MySQL...

Server concurrency estimation formula and calculation method

Recently, I need to stress test the server again....

How to quickly build your own server detailed tutorial (Java environment)

1. Purchase of Server 1. I chose Alibaba Cloud...

Native JS to achieve image marquee effects

Today I will share with you a picture marquee eff...

Use non-root users to execute script operations in docker containers

After the application is containerized, when the ...

Database index knowledge points summary

Table of contents First Look Index The concept of...

Implement MySQL read-write separation and load balancing based on OneProxy

Introduction Part 1: Written at the beginning One...

How to prevent duplicate submission in jquery project

In new projects, axios can prevent duplicate subm...

CentOS 7 Forgot Password Solution Process Diagram

need Whether it is a Windows system or a Linux sy...

JavaScript to implement the aircraft war game

This article shares with you how to use canvas an...

Docker Machine in-depth explanation

Differences between Docker and Docker Machine Doc...

Recommend 60 paging cases and good practices

<br />Structure and hierarchy reduce complex...