p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional JavaScript for Web Developers 3rd edition (http://p2p.wrox.com/forumdisplay.php?f=669)
-   -   Deferred attribute (http://p2p.wrox.com/showthread.php?t=99210)

brsastre October 4th, 2016 10:34 PM

Deferred attribute
 
From the book:

"HTML 4.01 defines an attribute named defer for the <script> element. The purpose of defer is to indicate that a script won’t be changing the structure of the page as it executes. As such, the script can be run safely after the entire page has been parsed. Setting the defer attribute on a <script>"

"Even though the <script> elements in this example are included in the document <head>, they will not be executed until after the browser has received the closing </html> tag. The HTML5 specification indicates that scripts will be executed in the order in which they appear, so the first deferred script executes before the second deferred script, and both will execute before the
DOMContentLoaded event
".


From Mozilla Developer Center

"The DOMContentLoaded event is fired when the document has been completely loaded and parsed"

So...

1. The script can be run safely after the entire page has been parsed.
2. ...and both will execute [B]before the DOMContentLoaded event
3. The DOMContentLoaded event is fired when the document has been completely loaded and parsed.

Isn't this a contradiction? 1 and 2 ?

Defer executes the script after (1) or before (2) the document has been parsed (3) ?

Thanks!

brsastre October 4th, 2016 10:38 PM

Deferred vs Async
 
Just reading the Deferred and Async attributes for the script tag..

What situations would be ideal for using the async attribute ?
I mean, I can't come up a situation where I would want the script to run before the html has been parsed.. Specially if there is code that make changes to the DOM, which is pretty common.

I would always go with defer I think..

Thank you !

brsastre October 4th, 2016 10:45 PM

Deferred attribute order
 
From the book:

"The HTML5 specification indicates that scripts will be executed in the order in which they appear, so the first deferred script executes before the second deferred script, and both will execute before the DOMContentLoaded event (see Chapter 13 for more information). In reality, though, deferred scripts don’t always execute in order or before the DOMContentLoaded event, so it’s best to include just one when possible."

So... will the first deferred script executes before, or after the second deferred script?

What situations could lead to the second deferred script get executed before the first deferred script ?

Is there a way to be completely sure that the scripts will run in the same order as they appear then?

Thank you!

brsastre October 4th, 2016 11:08 PM

Deferred attribute contradici
 
Chapter 2, Summary:

➤ All <script> elements are interpreted in the order in which they occur on the page. The code contained within a <script> element must be completely interpreted before code in the next <script> element can begin so long as defer and async attributes are not used.

➤ You can defer a script’s execution until after the document has rendered by using the defer attribute. Deferred scripts always execute in the order in which they are specified.

Isn't there a contradiction:

- The code contained within a <script> element must be completely interpreted before code in the next <script> element can begin so long as defer and async attributes are not used.
- Deferred scripts always execute in the order in which they are specified.

So.. does defer attribute guarantees the script to be executed in the order they are specified, or not?

Thank You!


All times are GMT -4. The time now is 12:17 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.