Home > Back-end >  why does html parser stop when meet script tag
why does html parser stop when meet script tag

Time:09-05

I heard that javascript can manipulate dom.
That's why parser stop when meet script tag.
But I cannot understand.
I want to know some examples.

CodePudding user response:

It has nothing to do with manipulating the DOM.

JavaScript can write to the document:

<h1>Example</h1>
<script src="example.js"></script>
<p>Fin</p>

with

 document.write('foo');

The HTML parser has to stop while the script is downloaded and executed in case it has something like that document.write statement. It has to parse foo into a text node before it reaches the <p> start tag.

Note that if you aren't writing parser blocking JS, you can use the defer attribute.

  • Related