Include js file into another js file

In the last days I had a big problem: how I can include a javascript file into another javascript file without use the tag script?
Searching on Google I have found the answer to my problem with this simple javascript function:

function addJavascript(jsname,pos) {
     var th = document.getElementsByTagName(pos)[0];
     var s = document.createElement(‘script’);
     s.setAttribute(‘type’,’text/javascript’);
     s.setAttribute(‘src’,jsname);
     th.appendChild(s);
}

the variable jsname is the name of the javascript file
the variable pos is the tag where you want to add the file

An example of usage:
addJavascript(‘newExternal.js’,’body’);
addJavascript(‘newExternal2.js’,’head’);

It works for me, I hope for you too!

[ the original news ]

Advertisements
This entry was posted in javascript. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s