Article directory
Summary of methods for introducing another js file into a js file
Examples test1.js
and test2.js
citations test1.js
_test2.js
Method 1: Put both JS in the same folder
Who is called, who is in front
<script src="./test2.js"></script>
<script src="./test1.js"></script>
Method 2: Create an anonymous class in jS to reference another JS
var myImport = function(){
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "libs/js/pingyin.js"); // 引用文件的路径
document.getElementsByTagName('head')[0].appendChild(script); // 引用文件
}
Add an initial loading method for test1.js
and reference it myImport()
.
window.onload = function(){
myImport();
}
Method 3: Add the following code at the top of the calling file
function addScript(url){
document.write("<script language=javascript src="+url+"></script>");
}
Method 4: Use export and import in es6 to achieve modularization
Import external variables or functions
// 导入变量或者函数
import {
firstName, lastName} from './test';
// 导入外部的模块
import './test'