Another option would be to use the readystatechange
event. The readystatechange
event fires when the readyState
attribute of the document
has changed. The readyState
attribute can be one of the following three values: 'loading'
, 'interactive'
, or 'complete'
. An alternative to using the DOMContentLoaded
event is to look for the readyState
to equal 'interactive'
inside of the document
's readystatechange
event, as in the following snippet.
document.onreadystatechange = function () {
if (document.readyState === 'interactive') {
// Execute code here
}
}
Although, in your case, the document
's readyState
seems to have already reached 'complete'
. In that case, you can simply swap 'interactive'
for 'complete'
in the snippet above. This is technically equal to the load
event instead of the DOMContentLoaded
event.
Read more on MDN, Document.readyState Document: readystatechange event