Adding external javascript file in javascript [duplicate]

南楼画角 提交于 2021-02-08 04:36:14

问题


I am writing an Electron program and want to use some javascript classes in the main process. I would prefer to have those classes in different files and include them inside of the script.

I usually did this by just adding both, the script and the classes inside the html file but since it is the main process there is not html file.

Question Is it possible to include an external javascript file in a different javascript file?


回答1:


Since electron is node based, you can use require both in your main process JS file and in any JS file your project uses.

// main.js or something
let myModule = require("./myModule");

// myModule.js
....
module.exports = function() {}; // or whatever you want to share with main.js



回答2:


jQuery has a method called getScript() which can be used to load external scripts, https://api.jquery.com/jquery.getscript/

$.getScript('https://example.com/js/another.js');


来源:https://stackoverflow.com/questions/46141419/adding-external-javascript-file-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!