3urdoch
3urdoch

Reputation: 7332

does a web browser hold onto a web server connection while inline javascript is executing?

For example if I wrote the following code containing a link to a 3rd party javascript while which took 1 second to load:

<!DOCTYPE>
<html>
<head>

</head>
<body>

  // note no async attribute!
  <script src="//thirdparty.com/some/slow/loading/script.js">
  </script>

</body>
</html>

Would the http connection to my web server be kept open until the the end of the document?

Update: I'm not talking in the context of Connection: Keep-alive, this would obviously retain a connection after the page has loaded. I am referring to the fact that the browser may not have fully read the contents of the document from the server at the point it executes the in-line javascript, so would it still retain its connection to keep reading the rest of the file, or would this have been read but not yet added to the DOM?

Upvotes: 1

Views: 138

Answers (2)

Guffa
Guffa

Reputation: 700582

No, the connection is not kept open until the document is completely parsed.

The document will continue to load while it's being parsed, and while the external script is requested, loaded, parsed and executed. The browser doesn't pause in the reading of the document just because it doesn't need any more data to parse right now, or because it's loading something else. It will still continue to load the document in the background.

Open connections is a more expensive resource than memory, so it's better for the browser to read all data into memory as fast as possible, instead of keeping connections open to read from them as data is needed.

Upvotes: 1

PurkkaKoodari
PurkkaKoodari

Reputation: 6809

The browser would first start loading your document. As soon as it has parsed the <script> tag it would make a connection to the third-party server and start loading the JS from there. The connection to your server will be closed when the loading of the main page is finished. However, the browser shows the loading icon as long as something (e.g. the JS) is loading, and it will also call window.onload after everything is loaded.

Upvotes: 0

Related Questions