Jump to content
  • Advertisement
Sign in to follow this  
MButchers

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

This topic is 4531 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!