"."> How to import JavaScript script files-Front-end Q&A-php.cn

How to import JavaScript script files

青灯夜游
Release: 2021-11-03 12:03:17
Original
3043 people have browsed it

In a web page, you can use the script tag and use the src attribute of the tag to introduce a JavaScript script file. The syntax is "

How to import JavaScript script files

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript script files refer to external JavaScript files that contain JavaScript code and have a ".js" extension.

If you want to introduce a JavaScript script file, you need to pass the script tag. The basic syntax format is as follows:

How to import JavaScript script files

In the above format, src is the attribute of the script tag, use To specify the path to an external script file. We can omit the type attribute, which can be abbreviated as:

How to import JavaScript script files

It should be noted that when calling an external JavaScript file, JavaScript script code can be written directly in the external JavaScript file. No need Write the import tag.

In actual development, when a large amount of JavaScript code with complex logic needs to be written, it is recommended to use JavaScript (external link type) by introducing a js script file. Compared with embedded, the advantages of external links can be summarized as the following two points:

1) Conducive to later modification and maintenance

Embedded will cause HTML and JavaScript code to be mixed together, which is not utilized For code modification and maintenance, external links will separate the three parts of code, HTML, CSS, and JavaScript, which will facilitate later modification and maintenance.

2) Reduce file size and speed up page loading

Embedding will embed all the JavaScript code used into the HTML page, which will increase the size of the HTML file and affect the web page itself. Loading speed, and external links can use browser cache to reuse JavaScript script codes that need to be used multiple times, which not only reduces the size of the file, but also speeds up the loading speed of the page. For example, when the same JavaScript file is introduced into multiple pages, the browser will cache the JavaScript file after opening the first page. The next time you open other pages that reference the JavaScript file, the browser does not need to reload the JavaScript file. file.

[Recommended learning:javascript advanced tutorial]

The above is the detailed content of How to import JavaScript script files. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!