Unfortunately many of these attempt to hijack the
window.onload = init;
That line of code will completely wipe out any other functions that were attached and ready to handle the onload event. How arrogant of your script to do so. Instead, your script should learn to play nicely.
window.onload += init;
But that won’t work. One approach I found on Simon Incutio’s blog (which is used by the original LightboxJS script) involves using a method that safely attaches an event handling method to the onload event without overwriting existing event handlers.
It works by checking to see if there any methods are already attached to the event. If so it attaches a new anonymous method that calls the original method along with the method you are attempting to attach.
Here is a snippet demonstrating this technique.
// Does stuff...
// Adds event to window.onload without overwriting currently
// assigned onload functions.
var oldonload = window.onload;
if (typeof window.onload != 'function')
window.onload = func;
window.onload = function()
This is pretty nifty, but there appears to be a whole new school of script libraries that provide this sort of functionality for attaching to any event, not just the
addLoadEvent method in each of them. But I think its time to allow a dependency on another script to avoid this duplication.