re1
re1

Reputation: 467

Is it faster to write <script defer> just before the </ head> tag than before the </ body> tag until the processing is completed?

Which of the following codes is faster to complete all processing? I thought that code H was faster, but when I tried it, code B was faster (I think ...). Depends on the environment?

code H

  <script src='js' defer></script>
</head>
<body>
</body> 

code B

  <script src='js' defer></script>
</body>

Upvotes: 0

Views: 48

Answers (1)

tkore
tkore

Reputation: 1151

That depends on how you define "faster". Faster to download? Execute?

By definition, scripts with defer always execute when the DOM is ready, but before the DOMContentLoaded event, regardless of where you put them. They would still need to be downloaded and only executed when they're supposed to - so the difference is almost unnoticeable.

Upvotes: 1

Related Questions