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

One line of code teaches you how to hide Linux processes

Friends always ask me how to hide Linux processes...

A brief analysis of MySQL locks and transactions

MySQL itself was developed based on the file syst...

Navicat connects to MySQL8.0.11 and an error 2059 occurs

mistake The following error occurs when connectin...

How to run a project with docker

1. Enter the directory where your project war is ...

jQuery achieves breathing carousel effect

This article shares the specific code of jQuery t...

Detailed explanation of this pointing problem in JavaScript

Preface The this pointer in JS has always been a ...

Several principles for website product design reference

The following analysis is about product design pr...

Common parameters of IE web page pop-up windows can be set by yourself

The pop-up has nothing to do with whether your cur...

How to modify mysql permissions to allow hosts to access

Enable remote access rights for mysql By default,...

Vue implements real-time refresh of the time display in the upper right corner

This article example shares the specific code of ...

How to understand the difference between computed and watch in Vue

Table of contents Overview computed watch monitor...