Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers 3rd edition
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers, 3rd Edition by Nicholas C Zakas; ISBN: 978-1-1180-2669-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers 3rd edition section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 4th, 2016, 11:34 PM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2016
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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!
Reply With Quote
  #2 (permalink)  
Old October 4th, 2016, 11:38 PM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2016
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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 !
Reply With Quote
  #3 (permalink)  
Old October 4th, 2016, 11:45 PM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2016
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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!
Reply With Quote
  #4 (permalink)  
Old October 5th, 2016, 12:08 AM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2016
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
replace attribute value on a node with another specific attribute value borgulas XSLT 3 September 16th, 2015 05:11 AM
Select nodes whose attribute matches another node's attribute? nikolai XSLT 4 February 4th, 2011 06:21 AM
Replace an attribute with another attribute georgemeng XSLT 8 June 10th, 2008 12:04 PM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 02:11 PM
get attribute value... clayman58 XSLT 1 August 9th, 2005 05:33 AM



All times are GMT -4. The time now is 03:58 PM.


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