最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - document.readyState on DOMContentLoaded? - Stack Overflow

programmeradmin3浏览0评论

In browsers that support the event DOMContentLoaded and the property document.readyState:

When DOMContentLoaded fires, can I assume that the value of document.readyState will always be either "complete" or "interactive"/"loaded"?

(Or could it be that document.readyState sometimes still has the value "loading"?)

In your answer please provide a reference to an authoritative source.

You may wonder: Why not just listen to readystatechange? It is because the Android 2.3.5 standard browser is a target platform, and it does not implement the readystatechange event.

In browsers that support the event DOMContentLoaded and the property document.readyState:

When DOMContentLoaded fires, can I assume that the value of document.readyState will always be either "complete" or "interactive"/"loaded"?

(Or could it be that document.readyState sometimes still has the value "loading"?)

In your answer please provide a reference to an authoritative source.

You may wonder: Why not just listen to readystatechange? It is because the Android 2.3.5 standard browser is a target platform, and it does not implement the readystatechange event.

Share Improve this question edited Sep 19, 2019 at 17:38 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Nov 12, 2012 at 15:41 fekleefeklee 7,6959 gold badges57 silver badges75 bronze badges 3
  • 2 Why not just listen to DOMContentLoaded? ;-) – Marat Tanalin Commented Nov 12, 2012 at 15:44
  • 1 I'm pretty sure Android 2.3 supports the readystatechange event, but just uses loaded instead of interactive/complete. I'll investigate and get back. – dotnetCarpenter Commented Oct 7, 2013 at 21:26
  • UPDATE - Indeed you're correct. I've made a small test here: test.it-kollektivet.dk/white-space/android2.3.html But the only value I get from document.readyState is loading and loaded. – dotnetCarpenter Commented Oct 7, 2013 at 21:45
Add a comment  | 

3 Answers 3

Reset to default 12

The value of the readyState property is always "interactive" when DOMContentLoaded has fired. This is evidenced by the fact that the MDN documentation claims:

// alternative to DOMContentLoaded event
document.onreadystatechange = function () {
  if (document.readyState == "interactive") {
    initApplication();
  }
}

is interchangeable with a DOMContentLoaded handler. You can also have a look at the spec here, which reiterates this.

As per accepted answer:

The value of the readyState property is always "interactive" when DOMContentLoaded has fired.

Wrong

It has either of:

  • interactive
  • complete

document . readyState ref.

Returns "loading" while the Document is loading, "interactive" once it is finished parsing but still loading subresources, and "complete" once it has loaded.

If one attach an event listener to readystatechange before Document has state interactive one can check for interactive alone, like with example from MDN. Then one will catch the state if it ever reaches it.

However if one check the state at a later stage it is not.

Also by example from MDN, these are equal:

document.onreadystatechange = function () {
  if (document.readyState === 'interactive') {
    initApplication();
  }
}


document.addEventListener("DOMContentLoaded", function () {
    initApplication();
});

That does not mean:

if (document.readyState !== 'loading')
    assert(document.readyState === 'interactive')

Which the answer suggests.

As to say:

  • document.addEventListener("DOMContentLoaded", ...

will never equal to:

  • window.addEventListener('load', ...

The value of the readyState property is at least "interactive" when DOMContentLoaded is fired. As @MTCoster pointed out here, the event is deferred until linked scripts with defer attribute and module scripts, linked or inline, have executed. See also this post.

发布评论

评论列表(0)

  1. 暂无评论