简体   繁体   English

如何将主Javascript文件导入其他文件?

[英]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.

相关问题 如何在其他JavaScript文件中导入JavaScript文件? - How to import javascript file in other javascript file? 如何使用rhino导入其他javascript文件 - How to import other javascript files with rhino 如何导入在 Vue cli 中使用其他本地文件中的函数的 JavaScript 文件而不会出现未定义的错误? - How do I import JavaScript files that uses functions in other local files in Vue cli without getting undefined errors? 如何在javascript主文件中导入外部模块(async.js) - How to import external module (async.js) in javascript main file 如何在ES6中创建主导入文件? - How do I create a main import file in ES6? 如何在 HTML 索引文件中导入多个 javascript 文件而不出现膨胀? - How do you import multiple javascript files in HTML index file without the bloat? 如何模块化JavaScript类(从主文件中拉出方法,将方法导入主文件) - How to modularize JavaScript class (pull methods out of main file, import methods into main file) 无法在其他 Javascript 文件中导入 Javascript 文件 - Cannot import Javascript file in other Javascript file 为什么其他JavaScript文件在我的PHP文件中不起作用? - Why do other JavaScript files not work in my PHP file? 如何从其他javascript文件导入函数? - How can I import functions from an other javascript file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM