Sign in to follow this  

[web] Including A .js file within another .js file

This topic is 4249 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, is there a simple way to include a javascript.js file within another .js file simular to the way you can import a stylesheet into another. I have looked at the 'import' statement but this doesnt seem to be what I'm after. Does anyone out there know if this can be done? and if so how? Many Thanks Mark

Share this post


Link to post
Share on other sites
There is no such function as far as I know, but this is because it's not really a neccesary functionality.

Either: use one massive JS file, or in the file that's using the JS file, include them in order of inheiritance ( b depends on a, so (script src="a")(/script)(script src="b")(/script) ).

As you'll find with larger javascript libraries (prototype, scriptaculous, rico, etc), they tend to do the latter. Just remember that you have to call the scripts in order of dependency and you'll be fine.

Share this post


Link to post
Share on other sites
You can do it with a hack like this:


function loadScript(file)
{
// Create script DOM element
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = file;

// Alert when the script is loaded
if (typeof(script.onreadystatechange) == 'undefined') // W3C
script.onload = function(){ this.onload = null; alert('Script loaded'); };
else // IE
script.onreadystatechange = function(){ if (this.readyState != 'loaded' && this.readyState != 'complete') return; this.onreadystatechange = null; alert('Script loaded'); }; // Unset onreadystatechange, leaks mem in IE

// Add script DOM element to document tree
document.getElementsByTagName('head')[0].appendChild(script);
};



Share this post


Link to post
Share on other sites
You can simply do this:


function jsInclude(url)
{
document.write('<script type="text/javascript" src="' + url + '"></script>');
}



The only pitfall is that you cannot access that included file until the calling file has loaded. Thus, this will not work:


jsInclude("mytest.js");

functionFromMyTest(); // hasn't been pulled in yet, so an error will be triggered.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by kanzler
You can do it with a hack like this:

*** Source Snippet Removed ***


Excellent technique. This is the truly cross browser "Dynamic Script Loader" script I have come across. Thanks very much.

Share this post


Link to post
Share on other sites
Quote:
Original post by konForce
function jsInclude(url)
{
document.write('<script type="text/javascript" src="' + url + '"></script>');
}

you mean,

function jsInclude(url)
{
document.write('<scr' + '' + 'ipt type="text/javascript" src="' + url + '"></scr' + '' + 'ipt>');
}

Share this post


Link to post
Share on other sites

This topic is 4249 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this