如何将javascript文件添加到另一个javascript文件 [英] How can i add javascript file into another javascript file

查看:86
本文介绍了如何将javascript文件添加到另一个javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将javascript文件添加到另一个javascript文件中.

例如2个文件one.js和two.js

现在我如何才能将two.js添加到one.js中?如果不是,请尝试使用脚本集的位置创建一个新的脚本元素,并将其添加到文档中.


  jsFirstMain.js */
函数 inc(文件名)
{
 var  body = 文档 .getElementsByTagName('  body').item( 0 );
脚本= 文档 .createElement(' 脚本');
script.src =文件名;
script.type = ' 文本/javascript';
body.appendChild(脚本)
}

// 示例
inc(" ); 


How can i add javascript file into another javascript file.

e.g. 2 files one.js and two.js

now how can i add two.js into one.js???

解决方案

If you''re using jQuery, jQuery.getScript() will do it. If not, try creating a new script element with the location of the script set and add it to the document.


/* jsFirstMain.js */
function inc(filename)
{
var body = document.getElementsByTagName('body').item(0);
script = document.createElement('script');
script.src = filename;
script.type = 'text/javascript';
body.appendChild(script)
}

//example
inc("jsSecondInternal.js");


这篇关于如何将javascript文件添加到另一个javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆