Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use defer with Google Maps Javascript?

The Google Maps javascript does some heavy DOM manipulation. Even so, the fine docs suggest to load it with the defer flag:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" async defer></script>

Why would the defer flag be suggested for a script that performs DOM manipulations? I ask to learn both about the defer flag and to learn about the Google Maps API as I seem to have a misunderstanding about what one of them is doing.

like image 439
dotancohen Avatar asked Apr 28 '16 08:04

dotancohen


People also ask

Why we use defer in JavaScript?

The defer attribute is a boolean attribute. If the defer attribute is set, it specifies that the script is downloaded in parallel to parsing the page, and executed after the page has finished parsing.

Should I use defer in script tag?

Defer attribute is useful when script is using for DOM manipulations. Means script will apply on document html. async attribute: It will download the script file and execute without wait the end of html parsing. In other words, It will not guarantee all the scripts will execute after the html parsing.

Why do I need an API for Google Maps?

The Google Maps API is one of those clever bits of Google technology that helps you take the power of Google Maps and put it directly on your own site. It lets you add relevant content that is useful to your visitors and customise the look and feel of the map to fit with the style of your site.


1 Answers

Normally, a script tag tells the browser to stop parsing the HTML, fetch the script, run it, and only then continue parsing the HTML. This is because the script code may use document.write to output to the HTML token stream.

async and defer are both mechanisms for telling the browser that it's okay to go ahead and keep parsing the HTML in parallel with downloading the script file, and to run the script file later, not right away.

They slightly different, though; this diagram from the script section of the WHAT-WG version of the HTML spec is useful for envisioning the differences:

enter image description here

Full details in the linked spec above, but in brief, for "classic" scripts (the kind you're used to; but module scripts are coming soon!):

  • Both async and defer allow the parsing of the HTML to continue without waiting for the script to download.
  • defer will make the browser wait to execute the script until the parsing is complete.
  • async will only make the browser wait until the script download is complete, which means it may run the script either before parsing is complete or afterward, depending on when download finishes (and remember it could come from cache).
  • If async is present and supported by the browser, it takes precedence over defer.
  • async scripts may be run in any order, regardless of the order in which they appear in the HTML.
  • defer scripts will be run in the order they appear in the HTML, once parsing is complete.
  • async and defer are well-supported in even semi-modern browsers, but are not properly supported in IE9 and earlier, see here and here.

Why would the defer flag be suggested for a script that performs DOM manipulations?

Two reasons:

  1. It allows the parsing to continue while the script is downloaded, and
  2. It means the script isn't run until parsing is complete.

If you didn't use defer and you placed your script tags non-optimally, using defer helps the API script behave properly by letting the browser finish building the DOM before the script tries to manipulate it.

A lot of people still put script tags in the head section of the document, even though that's usually the worst place to put them unless you use defer (or async). In most cases, the best place (unless you have a reason to do something else) is at the very end, just before the closing </body> tag, so that A) Your site renders quickly, without waiting for scripts; and B) The DOM is fully built before you try to manipulate it. Recommending defer may be saving them support hassles from people putting their script tags too early in the HTML.

like image 199
T.J. Crowder Avatar answered Oct 03 '22 21:10

T.J. Crowder