Can script.readyState be trusted to detect the end of dynamic script loading?

后端 未结 5 2030
醉话见心
醉话见心 2020-12-04 12:40

I use dynamic script loading to reduce the duration of the initial page load. To ensure that the functions and objects defined by a script are accessible, I need to ensure t

5条回答
  •  隐瞒了意图╮
    2020-12-04 13:10

    In Internet Explorer, the onreadystatechange handler fires as expected, after the end of the script.

    I performed the same test in Internet Explorer 6, Internet Explorer 7 and Internet Explorer 8, with similar results in these three browsers (here the logs recorded in Internet Explorer 6):

    Fri Dec 18 18:14:51 UTC+0100 2009
    Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
    Test for script.readyState behavior started
    Added script with onreadystatechange handler
    test1.js: Start
    test1.js: Start of closure
    test1.js: End of closure
    readystatechange: complete
    

    Here, with a test using a local file, the readyState is always "complete", and it was still the same after several page refresh.

    However, as noted in this post by Nicholas C. Zakas, you may also observe "loaded" and "complete", or just "loaded", under different circumstances.

提交回复
热议问题