Code inside DOMContentLoaded event not working

前端 未结 7 1561
一向
一向 2020-12-04 20:10

I have used




  


  
相关标签:
7条回答
  • 2020-12-04 20:37

    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

    0 讨论(0)
提交回复
热议问题