Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happened to the "<script defer>" hack in JQuery?

A few years ago Dean Edwards brought us this workaround to the document.onload problem. The IE version of the solution involved appending this snippet to the document:

<script defer src=ie_onload.js><\/script>;

Dean was also pretty adamant on the fact that this was the closest solution to perfection he could find and dismissed any solution that involved the onreadystatechange attribute as being unreliable (see comments).

Subsequent refinements on his solution still involved some version of <script defer> and most JS framework implemented it, including jQuery.

Today, I'm perusing JQuery 1.4.1's source and I can't find it.

At which point was it dropped and why?

like image 881
Michael Ekoka Avatar asked Oct 12 '10 11:10

Michael Ekoka


People also ask

Can I use defer for jQuery?

Deferred() method in JQuery is a function which returns the utility object with methods which can register multiple callbacks to queues. It calls the callback queues, and relay the success or failure state of any synchronous or asynchronous function.

Should I use script defer?

You should use defer for all other scripts. defer is great because it: Gets loaded as soon as possible — so it reduces load times. Doesn't execute until everything you need is ready — so all the DOM you need is there.

Should I async or defer jQuery?

In practice, defer is used for scripts that need the whole DOM and/or their relative execution order is important. And async is used for independent scripts, like counters or ads. And their relative execution order does not matter.

What does defer in script do?

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. Note: The defer attribute is only for external scripts (should only be used if the src attribute is present).


1 Answers

It was removed in the jQuery 1.2.2 release, you can find the release notes here.

Internet Explorer document ready drastically improved. We use a new technique inspired by Diego Perini. It allows us to not have to do a document.write() anymore, which is really fantastic.

Here's the 1.2.1 Version vs the 1.2.2 version.

The main motivation was to remove the document.write and avoid the problem of IE still triggering the ready a bit too early, so now it's completely rewritten to listen to onreadystatechange (in IE) and fall-back to window.onload if all else fails.

like image 146
Nick Craver Avatar answered Oct 10 '22 04:10

Nick Craver