...">

Html and defer attribute

Having the code below:

<html> <head> <script> function elem_onload() { console.log("elem_onload"); }; </script> </head> <body onload="elem_onload()"> <script type="text/javascript" src="script.js" defer></script> </body> </html> 

script.js

 console.log("external script"); 

defer attribute does not work. Output:

 external script elem_onload 

with or without a deferment attribute. Shoudn't it be

 elem_onload external script 

deferred determination?

Duplicate answer !?

I want to say that my answer is not duplicated

How exactly does <script defer = "defer"> work?

The recommended answer provided in the links refers to the built-in script where the browser behavior is clear to me - it just ignores defer . My question is about an external script, in which case the browser should execute an external pending script

after analyzing the document

as the documentation indicates, therefore, after the onload .

So I'm waiting for a suitable answer ...

+5
source share
2 answers

It's about onload . Here is the definition of the onload attribute taken from this page .

The onload attribute is triggered when an object is loaded.

onload is most often used in the body> element to execute a script after the web page has fully loaded all the content (including images, script files, CSS files, etc.). However, it can also be used for other elements (see "Supported HTML Tags" below).

Therefore, the onload function will execute after the contents loaded inside the body, that is, after the execution of the internal script.

0
source

I am afraid the remdevtec answer is incorrect, as it seems to have forgotten that I used the defer attribute in the script tag, while the definition says:

This Boolean attribute is set to indicate to the browser what the script is intended to be executed after parsing the document.

So, in this case (at least in my opinion) the external script should be executed after onload , but it is not.

0
source

Source: https://habr.com/ru/post/1240453/


All Articles