Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load timeout for Javascript

Tags:

javascript

dom

Consider the following document

<html>
    <body>
        This is some content
        <script type="text/javascript" src="verySlowToRespond.js"></script>
        This is some more content
    </body>
</html>

I'd like first to check my assumption that it is unsafe for the browser to parse beyond the script tag until the script has loaded and executed.

This means that (if my assumption is correct), say verySlowToRespond.js takes 20 seconds to respond, the page DOM cannot be fully assembled until this dependency is resolved.

Supposing verySlowToRespond.js hung about indefinitely? At what point would the browser give up and continue with the parse?

like image 685
spender Avatar asked Aug 02 '10 13:08

spender


2 Answers

Correct: the browser won't continue beyond the script tag until it's read and evaluated it.

The browser gives up based on the same timeout rules it uses for pages. It depends on the browser, and on the exact nature of the timeout.

I'm wondering why there'd be such a thing as a slow-to-respond script. Is there something wrong with your hosting? Is the script slow to respond, or does it load and then take a really long time to run?

like image 190
Pointy Avatar answered Sep 27 '22 17:09

Pointy


Yes your first assumption is correct. Browsers stop rendering until <script> tags finishing loading AND executing.

The behavior on very long running scrips is browser-dependant. Newer browsers will often give you a chance to abort the script. Older browsers may need to be force-closed.

like image 25
Kenan Banks Avatar answered Sep 27 '22 17:09

Kenan Banks