Jump to content

Multiple external JS files?


davej
 Share

Recommended Posts

I'm finally experimenting with some code that spans several JS files. I am curious about how windows.onload is normally handled since (AFAIK) you can only have one of those. What is the best or most standard way to handle several files that need initialization? I guess I could have each file append an init function name to a global array and then the onload function could call the list of init functions? That would be fancier than what I need but maybe that is what is done in larger projects?

Link to comment
Share on other sites

There's not a single way which is supported by all browsers to add another handler for an event, so you can use this instead:

if ((typeof window.onload) == 'function'){  old_handler = window.onload;  window.onload = function()  {    old_handler();    new_handler();  }}else{  window.onload = new_handler;}

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...