Home > Web Front-end > JS Tutorial > body text

What are the methods to introduce js into the page?

php中世界最好的语言
Release: 2018-04-18 15:18:35
Original
2120 people have browsed it

This time I will bring you the methods of introducing js into the page, and the precautions for introducing js into the page. The following is a practical case, let's take a look.

There are two basic ways to introduce js into the page: embedding js in the page, referencing external js files.

1. Embed js

in the page This is the simplest way to use js on the page. I usually use this method when writing a small test.

Write the script element in front of , and the content of the script element is the js code. Like this:

<script>
// 在这里写js
function test(){
alert('说点什么呢'); 
}
test();
</script>
Copy after login

2. Reference external js files

Referring to external js files can separate js files and HTML files, one js file can be used by multiple HTML files, and it is more convenient to maintain, etc.

The usage is to set the src attribute of the script element to the URL of the js file, such as:

<script src="js/test.js"></script>
Copy after login

3. The position of the script element in the HTML file

If you put the script element inside the head element, it would look like this:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="xxx.js"></script>
</head>
<body>
<!--页面内容-->
</body>
</html>
Copy after login

In this case, the page content will not be rendered until the browser downloads, parses, and executes the js file. If a page requires many js files, the browser may have a short "whiteboard" and the user experience is not good.

Therefore, we should put the js file in front of , so that the browser will display the page to the user first.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!--<script src="xxx.js"></script>-->
</head>
<body>
<!--页面内容-->
<script src="xxx.js"></script>
</body>
</html>
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:

Combining HTML tags with DOM nodes

js prohibits browser back events

JS click to cycle and switch to play pictures

The above is the detailed content of What are the methods to introduce js into the page?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template