Bug 39683 - DOMContentLoaded: firing before document loaded, upon user action
Summary: DOMContentLoaded: firing before document loaded, upon user action
Status: UNCONFIRMED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Page Loading (show other bugs)
Version: 528+ (Nightly build)
Hardware: PC OS X 10.5
: P2 Normal
Assignee: Nobody
URL: http://sprymedia.co.uk/media/misc/web...
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-25 12:40 PDT by Allan Jardine
Modified: 2010-05-25 14:26 PDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Allan Jardine 2010-05-25 12:40:15 PDT
I've recently noticed something very odd with DOMContentLoaded - if you have a large document which takes a while to load, but WebKit shows a link while the page is still loading which is clicked upon, then DOMContentLoaded fires! It takes you to the link, but only after listeners attached to the event are fired - and they expect the DOM to be loaded, which it isn't....

Here is an example of this in action: http://sprymedia.co.uk/media/misc/webkit/document_ready2.php . The page has a sleep(5) at the bottom of it, so click the link within 5 seconds to see the alert show up before the document has loaded. This occurs in both Webkit and Firefox, so presumably some implementation matching going on. Furthermore, testing it in IE shows something similar (but presumably window.onload?).

The following is a transcript from the Webkit IRC channel:

theallan2: Hello all! Noticing something very odd with DOMContentLoaded - if you have a large document which takes a while to load, but webkit shows a link while the page is still loading, then DOMContentLoaded fires! It takes you to the link, but only after listeners attached to the event are fired - and they expect the DOM to be loaded, which it isn't....
eseidel: theallan2: http://webkit.org/quality/reporting.html
theallan2: @eseidel: It's more of a question of is this a bug? Firefox shows exactly the same behaviour, so I guess it's been implemented that way...
eseidel: theallan2: I don't know
dhyatt: theallan2: well, there's a decent shot the page might finish parsing anyway before the link click gets processed
theallan2: eseidel: I'll bug it and see if anyone picks it up 
dhyatt: theallan2: all DOMContentLoaded means is that the page html has been parsed
dhyatt: it doesn't mean all the subresources have loaded
20:25
dhyatt: theallan2: you verified that the page wasn't fully parsed yet at the time the event fired?
theallan2: dhyatt: I've got a demo which shows this in action - there is a 'sleep(5)' in the PHP script to prevent the DOM from loading before you've had a chance to click the link at the top
dhyatt: theallan2: url?
theallan2: doh: http://sprymedia.co.uk/media/misc/webkit/document_ready2.php
jamesr: that is odd
dhyatt: i see
theallan2: it's not just me then 
dhyatt: well i see the bug
dhyatt: assuming it is considered a bug
dhyatt: if you hit Stop, we will fire DOMContentLoaded
dhyatt: if we stop a load we'll still fire it
dhyatt wonders if that is intentional
theallan2: interesting question... I can see a rational for doing that certainly, but it makes javascript functions run which are presuming that the DOM is now ready, since the event fired, but actually the DOM isn't ready - it hasn't fully loaded
20:30
jamesr: but if you hit esc on a huge slow page i would think you would want to be able to use the bit that did load
dhyatt: well onload doesn't fire if you stop a load
dhyatt: so it's kind of weird that domcontentloaded would
dhyatt: seems like a bug to me
theallan2: agreed - that would be the rational behind firing the event I guess. But what if the javascript is looking for stuff which was at the bottom of the page - error out...
jamesr: seems even weirder to fire it on a navigation
theallan2: heh okay - thanks very much guys - bug time...
dhyatt: we just fire it period if a load is interrupted jamesr
dhyatt: FrameLoader::stopLoad calls finishedParsing
dhyatt: which fires
dhyatt: doesn't care why
dhyatt: seems wrong
dhyatt: file a bug
Comment 1 Allan Jardine 2010-05-25 14:26:52 PDT
Mozilla bug discussing this issue: https://bugzilla.mozilla.org/show_bug.cgi?id=568050