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

问题描述

我正在寻找一种方法将一个JS文件直接加载到另一个,

*,而不必动态写入< scripttags。


有没有我可以使用

单个< scripttag调用一个外部JS文件的方法,但是将外部JS文件插入到ITSELF

其他五个内容中?


类似于:


/ * start external.js * /

import(" / js / script1 .js")

import(" /js/script2.js")

import(" /js/script3.js")

import(" /js/script4.js")

import(" /js/script5.js")

/ * end external.js * /


TIA

... Geshel

-

******* ******************************************* ******* ************

我的回复电子邮件地址是一个自动监控的垃圾邮件蜜罐。

除非你发送电子邮件希望被报道为垃圾邮件发送者。

请将所有电子邮件发送到我姓氏dot org的名字,然后用

作为NEO GESHELA的新闻报道? (全部大写)。

************************************* ************* *******************

I am seeking a method to load one JS file directly into another,
*without* having to dynamically write <scripttags.

Is there any method whereby I can call only one external JS file using a
single <scripttag, but have that external JS file insert into ITSELF
the contents of five others?

Something like:

/*start external.js*/
import ("/js/script1.js")
import ("/js/script2.js")
import ("/js/script3.js")
import ("/js/script4.js")
import ("/js/script5.js")
/*end external.js*/

TIA
...Geshel
--
************************************************** *******************
My return e-mail address is an automatically monitored spam honeypot.
Do not send e-mail there unless you wish to be reported as a spammer.
Please send all e-mail to my first name at my last name dot org, with
a subject-line of a??NEWSGROUP REPLY FOR NEO GESHELa?? (all uppercase).
************************************************** *******************

推荐答案



基于Hunlock的可爱小片段:




我写道:


函数embed_scripts()

{

var arg_obj = String(arguments [0]);

var arg =(arguments.length == 1)? arg_obj.split('',''):参数;

var head_obj = document.getElementsByTagName(''head'')[0];

for(var i = 0; i< arg.length; i ++)

{

var script_obj = document.createElement(''script'');

script_obj.type =''text / javascript'';

script_obj.src = arg [i] +''。js'';

head_obj.appendChild(script_obj );

}

}


它可以接受一个数组:


var _scripts = [''script1'',''script2''];

window.onload = embed_scripts(_scripts);


... 。或以逗号分隔的参数列表:


window.onload = embed_script(''script1'',''script2'');


现在,仅仅因为它对我来说完美无缺并不意味着它是防弹的。我可能没有

测试了所有情况,或者它可能是巧合的。


有一点可以肯定,它没有试图检查是否''script1''是一个有效的

文件。


让我们看看谁猛击我的头撞墙。 * grin *


-Lost

Based on Hunlock''s lovely little snippet:

http://www.hunlock.com/blogs/Howto_D...script_And_CSS

I wrote:

function embed_scripts()
{
var arg_obj = String(arguments[0]);
var arg = (arguments.length == 1) ? arg_obj.split('','') : arguments;
var head_obj = document.getElementsByTagName(''head'')[0];
for (var i = 0; i < arg.length; i++)
{
var script_obj = document.createElement(''script'');
script_obj.type = ''text/javascript'';
script_obj.src = arg[i] + ''.js'';
head_obj.appendChild(script_obj);
}
}

It can accept either an array:

var _scripts = [''script1'', ''script2''];
window.onload = embed_scripts(_scripts);

....or a comma-separated list of arguments:

window.onload = embed_script(''script1'', ''script2'');

Now, just because it worked flawlessly for me does not mean it is bulletproof. I may not
have tested all situations or it could be coincidental that it works.

One thing for certain, it makes no attempt to check whether or not ''script1'' is a valid
file.

Let''s see who "slams my head against the wall." *grin*

-Lost




我在这一行的函数名末尾忘了''s'。


< snip> ;


-Lost

I forgot the ''s'' at the end of the function name on this line.

<snip>

-Lost




基于Hunlock的可爱小片段:



[.. 。$

[...]



一个小小的研究,线程createTextNode和IE7:


< URL:

browse_frm / thread / 7e23f42490c301de / 3441a1cc21869a10?

lnk = gst& q = createTextNode + IE + Randy + Web& rnum = 1& hl = en#3441a1cc21869a10>


-

Rob

A little research, thread "createTextNode and IE7":

<URL: http://groups.google.com.au/group/comp.lang.javascript/
browse_frm/thread/7e23f42490c301de/3441a1cc21869a10?
lnk=gst&q=createTextNode+IE+Randy+Web&rnum=1&hl=en #3441a1cc21869a10 >

--
Rob


这篇关于将一个外部JS文件直接包含在另一个中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:48