P
pcx99
That's a really nice mod! Clipped for the toolbox.
Thanks!
Thanks!
-Lost said:I am seeking a method to load one JS file directly into another, *without* having to
dynamically write <script> tags.Is there any method whereby I can call only one external JS file using a single <script>
tag, but have that external JS file insert into ITSELF the contents of five others?
Based on Hunlock's lovely little snippet:
http://www.hunlock.com/blogs/Howto_Dynamically_Insert_Javascript_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 + '.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