Document.readyState:
The Document.readyState
aims to identify the upload status of a document. It is used to interact with the page in certain loading states. It is used based on notable reference events by performance tools and Apis. This metric is reported by browsers.
Document.readyState
has three possible response states (Loaded, Interactive, complete):
Loaded:
Informs if a document is being loaded (Loaded).
loading:
States at run time of readyState:
That is, when the document.readyState
has a "loading" status, meaning that the document (for example an index.html type html file) is being parsed and treated to provide a state.
Other page sub-features are not yet a factor because the initial document is just starting to parse, so the browser doesn’t know if they exist.
The html itself is currently downloaded (the browser has html), but is being processed...
The date/time stamp of the "domLoading" navigation sync API is triggered immediately before the document has a loading status.
Interactive:
When document.readyState
has an "Interactive" status, meaning that the document is now parsed and loaded, but sub-features like CSS and images are still being uploaded or analyzed. When document.readyState
reports that it is interactive, also means that the event DOMContentLoaded
was triggered.
Interacting with the readyState:
For each change in Document.readyState (for example, if readyState changes from "loading" to "interactive"), an event readystatechange is triggered in the document object. Using the event readystatechange, we can interact with these "changes".
Example:
// alternativa para carregar o evento
document.onreadystatechange = function () {
if (document.readyState == "complete") {
initApplication();
}
}
complete:
When document.readyState
has a "complete" status, meaning that the document is now parsed and loaded and all known document sub-resources, such as CSS and images, have been parsed and uploaded. When Document.readyState reports that it is complete, it also means that the load event has been triggered.
In human terms:
Document.readyState
tells us what stage the page loading is at. It’s the browser version of "Are we there yet?" (We’ve arrived.?).
This information can be used to interact with the page while it is at different stages of loading a page.
Briefly: in general, you will only use the readyState
as a state conditional, to interact with your object being rendered...
A way to capture all possible events of a given event with the document.readyState
, in case I am capturing events of a GET request by ajax:
function handler() {
console.log(this.readyState)
//0 (não inicializado => uninitialized | esse é antes de qualquer estado, então ele não será impresso aqui dentro, pois ele não passa pelo onreadystatechange)
//1 (a carregar => loading)
//2 (carregado => loaded)
//3 (interactivo => interactive)
//4 (completo => complete)
}
var client = new XMLHttpRequest();
client.onreadystatechange = handler;
client.open("GET", "https://www.google.com");
client.send();
Curiosity: If, for example, you run the above code only on your browser console, probably the 1
and the 4
will only be defined, because 2
and 3
are not only seen by the console.
Related (possibly duplicate): https://answall.com/q/232001/132
– Victor Stafusa
No, the one from my comment up there is in the case of AJAX, not the page load.
– Victor Stafusa
That’s right @Victorstafusa, the API interface of both is quite similar, and they even use similar property names, but the behavior really isn’t exactly the same.
– Guilherme Nascimento
These are the States ancient and this the new. On the same link with the new ones, there is a table with
Browser compatibility
. However, I have encountered several difficulties in using only the eventReadyState
to check if a page has been opened completely, it does not work every time. I always needed to add some loading time along withReadyState
– danieltakeshi