本文介绍了在.js文件中包含.js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在另一个 .js 文件中包含 .js 文件?

I'd like to know if it is possible to include a .js file within another .js file?

我想要这样做的原因是将客户包括在内。我有几个 .js 文件已经用客户端需要的函数编写。客户端将有一个html文件,他/她使用 .js 文件包含(我的 .js 文件) 。

The reason for me wanting to do this is to keep client includes to a minimum. I have several .js files already written with functions that are needed by the client. The client would have an html file which he/she manages with a .js file include (my .js file).

我可以用其中的所有函数重新编写一个新的 .js 文件,或者为了避免双重工作,想办法写一个包含其他 .js 文件的 .js 文件。

I could re-write a new .js file with all the functions in it or, to avoid doing double work, figure out a way to write a .js file that includes other .js files.

推荐答案

我基本上这样做,创建新元素并将其附加到< head>

I basically do like this, create new element and attach that to <head>

var x = document.createElement('script');
x.src = 'http://example.com/test.js';
document.getElementsByTagName("head")[0].appendChild(x);

您还可以对每个人使用 onload 事件你附上的脚本,但请测试一下,我不确定它是否可以跨浏览器工作。

You may also use onload event to each script you attach, but please test it out, I am not so sure it works cross-browser or not.

x.onload=callback_function;

这篇关于在.js文件中包含.js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:33