[Greasemonkey] What is window.addEventListener('load', ...) required?

Mark Pilgrim pilgrim at gmail.com
Fri Apr 15 09:37:07 EDT 2005


Subject says it all: under what circumstances would you want to delay
execution of some portion of your script until window onload?  I ran
across this comment in BetterDir, written at about 4 AM:

    // GreaseMonkey mythology states that wholesale DOM changes should
be done on load, rather than in the GM script itself -- dunno if this
is still true, or indeed if it was ever true, but a little chicken
sacrifice never hurt anyone except the chicken
    window.addEventListener('load', 
        function() {
	    document.body.appendChild(table);
	    if (footer) {
		document.body.appendChild(footer);
	    }
	},
        true);

( http://diveintomark.org/projects/greasemonkey/betterdir.user.js )

In this case, it was to add elements to the page after completing
wiping out the page earlier with document.body.innerHTML = "".  I ran
into similar problems while writing Butler, specifically the part
which adds the "enhanced by butler" banner along the top of the page
(a styled div with some text and links).  If I tried to add that to
the DOM during the script itself, entire pages ended up rendering
duplicated.  Or sometimes it was just the last form on the page (like
at the bottom of the search results).  I assume this is some obscure
Firefox rendering bug, but I never did figure out exactly what caused
it, and I opted for a hackish solution of using a setTimeout with a
2-second delay (1 second still triggered the bug... sometimes).

-- 
Cheers,
-Mark


More information about the Greasemonkey mailing list