Summary of methods for introducing another js file into a js file

Summary of methods for introducing another js file into a js file

Examples test1.jsand test2.jscitations 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.jsand 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'

Guess you like

Origin blog.csdn.net/weixin_35773751/article/details/132432768