On a recent project I took my previous approach to automating firing of onload events to a new level.
For instance if your code was architected in an object literal such as:
1 2 3 4 5 6 7 8 9 10 11 |
|
A page with this body tag:
1
|
|
would load these functions sequentially:
UTIL.fire is calling: FOO.common.init() UTIL.fire is calling: FOO.shopping.init() UTIL.fire is calling: FOO.shopping.cart() UTIL.fire is calling: FOO.common.finalize()
In addition, using these classes and IDs on the body tag provides some excellent specific hooks for your CSS.
The javascript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
This system worked very well and keeps you in serious control of the execution order.
In the end, I used this plus a custom event to bind super low priority script. For example:
1
|
|
And I’d trigger that
1
|
|
at the very end of UTIL.loadEvents(). This allows you to keep similar code together, but delay portions responsibly without any setTimeout ugliness.
So Blake Waters presented this topic in August this year.
And now Jason has published his approach on the most excellent Viget Inspire blog. Please check it out.
It’s essentially using data-attributes instead of classes to trigger this action.. So..
1
|
|
Will fire off:
FOO.common.init(); FOO.users.init(); FOO.users.show();
Boom boom bam. I dig it. Check it out in its full glory.