views:

241

answers:

1

Hi all, I'm working on a page with 60+ images and the dom:loaded event isn't firing until the last image request is initiated (not fully loaded, which is expected). On a slower connection (using some throttler to simulate this) it is very noticable. The dom:loaded event fires something that will kick in javascript interaction on the page, which I want to be as early as possible.

This doesn't seem like expected behavior because the image tag is already rendered on the page (its in the page source). Or is it?

+1  A: 

No, the <img> tag is not considered fully rendered by the browser until the image file is fully downloaded. And yes, this is documented and expected behavior.

If you want an onDomReady functionality in prototype, have a look at this script: http://www.skyrocket.be/download/prototype.domReady.js

Another alternative, and some people don't like this so I'm risking downvoting for saying this, is to put your script at the end of the document. Javascript is executed as it is encountered and the document is built the same way so a block of javascript at the end of the document means that the script can access all DOM nodes above it.

Note that having a script block in the document body is perfectly valid and correct according to HTML specs and your page will validate if you do it. In fact both Yahoo and Google recommend putting script blocks at the end of the document to improve page rendering time. Some people somehow feel it is ugly to do this. And I used to feel the same. But beauty is in the eyes of the beholder.

slebetman
Prototype already has something that handles this which is document.observe('dom:loaded', function() { dostuff(); }. Which is what is being used.It fires DOMContentLoaded on supporting browsers once the dom is loaded but before images are loaded. What I'm asking is why this event doesn't fire until the request for the final image is initiated. It must have something to do with how the DOM tree is created.
Justin
Not sure, but looking at the domready script it looks like it does something different in IE. Anyway edited my post to give another way to do this if all else fails.
slebetman
thats because IE doesnt fire DOMContentLoaded. the typical way to figure out a 'domready' event for IE is to use `setInterval` with a `doScroll()` call. That won't work until the DOM is ready.
seanmonstar
The hideous `doScroll` hack can fail in some circumstances. Are you using iframes? What browsers are waiting for full onload?
bobince