"."/> ".">
Home >Web Front-end >Front-end Q&A >How to import JavaScript script files
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 "
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:
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:
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!