When does $(document).ready() fire?

前端 未结 5 1791
栀梦
栀梦 2020-12-30 13:58

The comments from this question got me thinking about something. When exactly does the $(document).ready() function fire? The obvious answer would be \"when the

5条回答
  •  抹茶落季
    2020-12-30 14:16

    For instance, if I turned output buffering on and flushed my output while PHP continued executing, wouldn't that send output to the browser?

    Yes, it'd send output, but it doesn't mean the browser thinks the server has finished. I know it's not PHP, but I love the Perl article Suffering from Buffering. The document is ready when the UserAgent thinks it's ready. However, the browser will keep the socket open, while it still thinks it's receiving data and no END signal has been sent.

    So is there any way the document could be ready before the PHP script has finished executing, or does the event wait until the request has finished?

    Typically the browser will wait until the server finishes sending data. If you're flushing the output, it's possible the web server can timeout while the script is still running, for instance, I think Apache has a default of 2 minutes. If the server sends the end signal, your document has finished and your browser may prepare the DOM and fire the DOMReady event, even if your script is still running on the server.


    Contrary to some other comments and are not good indicators of the DOM being ready, primarily because a page may be malformed (have misspellings or not include those end tags). In those cases, the browser will still render in Quirksmode and fire the DOMReady.

提交回复
热议问题