I'm trying to help developing a library and for it I'm trying to work with page loading.
In the process I want to make the library completely compatible with the use of defer and async.
What I want is simple:
How can I know that DOMContentLoaded was fired by the time the file is executed?
Why is this so difficult?
In IE, document.readyState show interactive before DOMContentLoaded.
I won't use browser detection in any way, it's against the policy of me and the rest of the participants.
What's the correct alternative?
Edit:
Seems like I wasn't clear enough. I'm not interested to know if the load event has already occurred!!! I already knew how to solve that problem! I want to know how to solve with DOMContentLoaded!!!
If you want to know "exactly" if DOMContentLoaded was fired, you could use a boolean flag like this:
then check with:
Here is the thing, if some other library (such as jQuery) already used DOMContentLoaded, you can't use it again. That can be bad news, because you end up without being able to use it. You are gonna say, why not just use
$(document).ready()
, because, NO!, not everything needs to use jQuery, specially if it is a different library.Try this or look at this link
Note
If you have a
<script>
after a<link rel="stylesheet" ...>
the page will not finish parsing - and
DOMContentLoaded
will not fire - until the stylesheet is loadedThe
document.readyState
is changed when this event is fired. So you can check thereadyState
value and this way tell if the event was fired or not. Here's an code to runstart()
when document is ready for it:For seeing if all resources in the page have been loaded:
This has been supported in IE and webkit for a long time. It was added to Firefox in 3.6. Here's the spec.
"loaded"
is for older Safari browsers.If you want to know when the page has been loaded and parsed, but all subresources have not yet been loaded (which is more akin to
DOMContentLoaded
), you can add the "interactive" value:Beyond this, if you really just want to know when DOMContentLoaded has fired, then you'll have to install an event handler for that (before it fires) and set a flag when it fires.
This MDN documentation is also a really good read about understanding more about the DOM states.