The defer attribute tells the browser that the script doesn't have to be run immediately (i.e., it doesn't use document.write). That means that it can start fetching the script immediately when it sees the script tag *and* continue parsing the document.
Without the defer attribute, the browser must stop parsing the document until the script has completed loading and been run, because the script might use document.write to change how the parsing should continue. If you put the script at the end of the body, the browser won't start fetching it until it has parsed the script tag. That introduces an extra delay before the script is ready and run - the time it takes to parse the document and create the DOM for it. By putting the script tag early but with the defer attribute can reduce the latency of your page - the time it takes before the onload event fires - by fetching over the network (or even from local cache), which is I/O bound, while it parses the document (which is memory/CPU bound). But then, AFAIK, not all browsers handle the defer attribute the way it was intended, so it's no panacea. /L On Mon, Nov 7, 2011 at 1:59 PM, Timothy J. Warren <timw4m...@gmail.com> wrote: > I still don't really understand the "defer" attribute, but I also don't see > the advantage, when you can just put your scripts as late in the document as > possible. > > On Monday, November 7, 2011 7:50:18 AM UTC-5, Lasse Reichstein wrote: >> >> On Thu, Nov 3, 2011 at 8:36 PM, dtang85 <dta...@gmail.com> wrote: >> > I know that it is considered best practice to place your scripts at >> > the bottom of the page before the closing body tag. However, I started >> > using Require.js recently and the Google Maps API, and both tell you >> > to place your scripts in the head. Is there a reason for this or do >> > the examples just put it there because this was common practice in the >> > past? >> >> If you use the "defer" attribute on the script tag, it makes sense to put >> it as >> early in the document as possible. Then the script can be loaded in >> parallel with >> the page being parsed. >> >> /L > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/jsmentors@jsmentors.com/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/jsmentors@googlegroups.com/ > > To unsubscribe from this group, send email to > jsmentors+unsubscr...@googlegroups.com > -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com