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

How to create a trigger in MySQL

This article example shares the specific code for...

How to install and modify the initial password of mysql5.7.18

For Centos installation of MySQL, please refer to...

Detailed explanation of the use of Linux lseek function

Note: If there are any errors in the article, ple...

How to use JS to implement waterfall layout of web pages

Table of contents Preface: What is waterfall layo...

Detailed graphic tutorial on how to enable remote secure access with Docker

1. Edit the docker.service file vi /usr/lib/syste...

Detailed explanation of mysql user variables and set statement examples

Table of contents 1 Introduction to user variable...

Flex layout makes adaptive pages (syntax and examples)

Introduction to Flex Layout Flex in English means...

Analysis of the ideas of implementing vertical tables in two ways in Vue project

Problem Description In our projects, horizontal t...

How to implement Nginx configuration detection service status

1. Check whether the check status module is insta...

Docker Swarm from deployment to basic operations

About Docker Swarm Docker Swarm consists of two p...

How to check whether a port is occupied in LINUX

I have never been able to figure out whether the ...

A complete tutorial on installing Ubuntu 20.04 using VMware virtual machine

Ubuntu is a relatively popular Linux desktop syst...