Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a browser not always finishing rendering of the preceding HTML, before executing JavaScript?

The question is about the following code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Test</title>
</head>
<body>
    One line of HTML code
    <script>
        // Synchronous delay of 5 seconds
        var timeWhile = new Date().getTime();
        while( new Date().getTime() - timeWhile < 5000 );
    </script>
</body>

I tested it in Firefox and Chrome and they are showing (rendering): "One line of HTML code" after 5 seconds and not within 5 seconds. Why is a browser doing that?

I understand why a browser has to stop rendering when executing JavaScript, because you can change the style of elements with JavaScript (as an example). It would give problems if the browser has to show and change content exactly at the same moment. That's why a browser is blocking rendering while executing JavaScript.

In the example above when starting with the executing of JavaScript, "One line of HTML code" is already parsed by the "HTML parser". It has to, because JavaScript can contain for example document.write, so then the appended string has to come after the preceding HTML. Apparently there is some time between "parsing HTML" and showing / rendering that same HTML, because otherwise the browser in this example would already show something within 5 seconds, but that's not the case.

When you replace "One line of HTML code" by a lot of HTML code then the browser will already show some content within the 5 seconds, so in principle it's possible to show already some content.

If I would be a browser then I would do:

  • Parse "One line of html code"
  • Seeing some block of JavaScript
  • Finish rendering the HTML, preceding the "JavaScript block", so the browser will show at this point: "One line of HTML code"
  • Now pause rendering and execute the JavaScript code.
  • After executing the JavaScript code, start rendering again.

In an example like this, the browser could show some content 5 seconds earlier. That's a big speed gain in terms of rendering.

Maybe it's something that browsers can improve, but maybe there is another reason. Maybe someone knows more about it and can explain me that.

like image 574
Maarten Bruins Avatar asked Oct 25 '17 11:10

Maarten Bruins


People also ask

How does a browser handle JavaScript and HTML?

The script sections of a web page are handled by the browser's JavaScript interpreter, which may be an intrinsic part of the browser but usually is a distinct module, sometimes even a completely distinct project (Chrome uses V8; IE uses JScript; Firefox uses SpiderMonkey; etc.).

Why is my JavaScript not working in HTML?

On the web browser menu click on the "Edit" and select "Preferences". In the "Preferences" window select the "Security" tab. In the "Security" tab section "Web content" mark the "Enable JavaScript" checkbox.

Does browser render the HTML it gets as response?

Response. Once we have an established connection to a web server, the browser sends an initial HTTP GET request on behalf of the user, which for websites is most often an HTML file. Once the server receives the request, it will reply with relevant response headers and the contents of the HTML.

How does browser render JavaScript?

When the browser reads HTML code, whenever it encounters an HTML element like html , body , div etc., it creates a JavaScript object called a Node. Eventually, all HTML elements will be converted to JavaScript objects.


1 Answers

Try to externalise the inline javascript which you have in above example.

In the inline script, the time is taken up running the script, which might change the DOM. Trying to render the DOM while it's mutating is a recipe for a mess. So rendering only happens at points when the JS is stalled, and therefore the DOM is stable.

While waiting for an external script to download, the running of scripts is stalled, so the DOM can be rendered safely. The downloaded JS won't be run until the rendering is complete.

Hope this Helps!

Regards, Eby

like image 50
Eby Jacob Avatar answered Oct 23 '22 22:10

Eby Jacob