[英]How do import a main Javascript file into other files?
I currently have separate Javascript code for each HTML page I have. 我目前对每个HTML页面都有单独的Javascript代码。 I wrote some JavaScript code that I use frequently for HTTP requests to my Node server and it's my understanding that the code would run more efficiently if I moved this code to a central file and referenced it elsewhere. 我编写了一些JavaScript代码,这些代码经常用于对Node服务器的HTTP请求,并且据我了解,如果将代码移至中央文件并在其他位置引用,则代码将更有效地运行。 How can I import this file into another? 如何将该文件导入另一个文件? Also, will doing so result in any more efficiency or will it just look cleaner? 此外,这样做会提高效率还是看起来更干净?
You can easy import file to another file in node.js
, but you want to import it in front-end, so you can only include a script file in an HTML page, not in another script file. 您可以轻松地将文件导入到node.js
另一个文件,但是您想要在前端导入它,因此您只能在HTML页面中包含脚本文件,而不能在另一个脚本文件中包含脚本文件。 You can write JavaScript which loads your "included" script into the same page using the following: 您可以编写JavaScript,使用以下方法将“包含的”脚本加载到同一页面中:
var imported = document.createElement('script');
imported.src = '/path/to/imported/script';
document.head.appendChild(imported);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.