Detect if a Document Has Loaded with JavaScript
If you follow me on Twitter, you've probably noticed me whining about ChromeDriver. For some reason it seems as though tests run before the document has properly loaded, leading to transient test failures and loads of frustration.
I thought the best way to avoid these problems was to ensure the document had loaded before each test run -- that way there's no excuse for transient loading problems. Here's the snippet I use to check if the page is ready:
// The basic check
if(document.readyState === 'complete') {
// good to go!
}
// Polling for the sake of my intern tests
var interval = setInterval(function() {
if(document.readyState === 'complete') {
clearInterval(interval);
done();
}
}, 100);
I found it ironic that for years we went looking for the ultimate "domready" script and here I am in 2015 trying to figure out if the document has completed loading. This is why we drink.
CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...
I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener
and attachEvent
. Times have changed but there are still a few functions each developer should...
If you're still rocking an iPhone and fancy taking a photo every now and then, you'd be crazy not to be using an app called Instagram. With Instagram you take the photos just as you would with your native iPhone camera app, but Instagram...
We've all inherited rubbish websites from webmasters that couldn't master valid HTML. You know the horrid markup: paragraph tags with align attributes and body tags with background attributes. It's almost a sin what they do. That's where dwMarkupMarine comes in.
Between
DOMContentLoaded
/load
anddocument.readyState
, I think that there are a few ensure that something is executed once the document is loaded.Nice, Chris!
Thanks. I think you may have posted something very similar to this before. Or am I just imagining that?
I must be missing something obvious … why not just use jQuery?
You don’t use jQuery for a tiny code snippet…
Re use is cultural fenomenon Unless this is some kind of deep JavaScript master class? Which I think it is not.
We have solved and debated this issue in 2008 last time.
As I like to reuse what is shareable, I might point to this (instead of re writing it all over again).
No it was 2006 sorry … Very interesing page to read now.
I suggest read it all, including comments, than come back here.
Personally, I will prefer js and only include jQuery if there is a strong need and justification for that. But that’s me, you might prefer to use jQuery, but i don’t think there is a need to get pissed for using js instead of jquery
Alternately, I think you could use the
ReadyStateChange
Event.A few people mentioning using an event listener. The problem is that I don’t know if the page is done loading; i.e. I can’t count on an event listener. Polling is the best way if I don’t know when my script is being injected.
Could you clarify this a litte? Using polling here seems a bit crude in comparison to replacing it with listening for a
readystatechange
event.If my test is triggered after the page is ready (remember, I can’t count on when it starts), readystatechange will have already triggered and my signal will never be triggered.
But wouldn’t that be covered by your basic test already? So…only bind a listener in case the
readyState
isn’tcomplete
yet?Maybe a combination of the two would suit
This is the version that I am using at the moment, I don’t know if it applies to your test though..
How can i use this for a preloader?
jQuery code is:
but i will use it without jQuery…
thanks! worked perfectly for my case where no events seem to work as well. just wanted a script for console or bookmark which would keep scrolling down on infinite scrollers. it surely still can be improved, but this was good enough for me for now:
if (document.readyState === 'complete')
Yep, that worked for me. Tough to find because there’s way more out there on the event ‘ready’ as opposed to the state ‘ready’.