The Blog

Multiple, synchronous, onload functions

There’s not a single serious web site I did, where I haven’t combined two or more scripts. Calendars, DHTML layers, cookies…they all have one thing in common – they want to grab window.onload for them self.
I usually dealt with that by changing the onload function for last loaded script. This was a pain when for some reason I needed to change the loading order.

When I got tired of it, I wrote a custom script, that’s loaded before all others, and gathers all of them into one piece.

Internet Explorer 5 and later have attachEvent method for all objects, but I wanted a script to work in all browsers. First version I wrote was dealing with just non-IE5+ browser – I used existing method for IE5+. After first real use, I realized that IE5 does not execute functions in the exact order they were added, so I removed browser-detection (an added benefit) and used this script for all.

Include this script file first, and then add other scripts’ onload functions like this:

AE_AttachEvent("onload", "function_name")

function_name should not include brackets (and thus no arguments can be passed). I wrote this script long time ago, so I don’t remember exactly why is this necessary (I think that eval call went crazy with it).

Scripts also supports onresize, and no other events, and it works only with window object. If you need other events, just add corresponding AE_WindowEvent() function, similar to existing AE_WindowOnload().

If you need this for objects other than window, write the functions from above, and add needed line(s) of code to AE_AttachEventSetup() function.

Or you can completely extend the script to work with any object, any event. Your call; this worked for me.

Banca

Banca

Beautiful and functional currency converter, supports just about any currency in the world.

Go Couch to 5k

Go Couch to 5k

The most popular starter running program in beautiful feature-rich app (GPS tracking, charts, detailed history etc)

Quickie to do

Quickie to do

The fastest short-term task-list / check-list app on the App Store. Really.

Guerrilla Cardio

Guerrilla Cardio

The most challenging high-impulse interval training in the world.

Run Mate

Run Mate

A versatile running coach app, with unlimited number of running programs. Perfect for casual runners.

3 Comments

Feel free to chime in, looking forward to it. Leave a Comment

  1. Funny, I worked on this as well. But I also wanted to unify the event-model, so every event property exposed by my library would be availble on every browser.

    I never finished it though, because a) I didn’t actually need the script, and b) somewhere I found an other guy almost done the same as I did (he finished it, but didn’t really unify the eventmodel).I can’t find his URL at the moment.

  2. jason says:

    where do i put the AE_AttachEvent(“onload”, “function_name”)…in the tag?

    i am trying to have a clock and a menu and both use body onload functions

  3. Aleksandar says:

    Jason, put it at the bottom of each script. And I hope you don’t use backslashes — this is some WordPress stupidity that I’m gonna figure out and remove.

Comments are now closed for this article.