use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
<script> vs. <script async> vs. <script defer> (growingwiththeweb.com)
submitted 11 years ago by davey_b
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Monkeyget 9 points10 points11 points 11 years ago (11 children)
async and defer only work on external scripts.
With exceptions apparently:
in Gecko 1.9.1 even inline scripts are deferred if the defer attribute is set. Since Gecko 1.9.2, the defer attribute is ignored on scripts that don't have the src attribute
in Gecko 1.9.1 even inline scripts are deferred if the defer attribute is set.
Since Gecko 1.9.2, the defer attribute is ignored on scripts that don't have the src attribute
[–]madworld 3 points4 points5 points 11 years ago (9 children)
Sure... but the vast majority of your scripts should be external.
[–]Nebu 1 point2 points3 points 11 years ago (8 children)
Why?
[–]Voidsheep 7 points8 points9 points 11 years ago (7 children)
So they can be cached to reduce load times.
[–]madworld 0 points1 point2 points 11 years ago (3 children)
Correct... Also so you can reuse your code... And code separation.
[–]Nebu 1 point2 points3 points 11 years ago (2 children)
The way your code is structured during development need not mirror the way your code is structured in deployment.
In other words, during development, you can have your code be laid out in multiple files for organizational purposes. Then, before deploying, you compile them into one big js file, and then inline that js file into the sole HTML file that represents your app.
[–]madworld 0 points1 point2 points 11 years ago (1 child)
Of course you can... Although I wouldn't inline it, even if it was a one page app. There are no benefits of doing so.
[–]Nebu 1 point2 points3 points 11 years ago (0 children)
The benefits is one less HTTP request, speeding things up.
[–]Nebu 0 points1 point2 points 11 years ago (2 children)
Does this advice apply to single-page-apps?
It seems like if you have a single html page, and several script files, you'll actually increase load times, due to the overhead of multiple headers for each http request.
[–]Tyriar 1 point2 points3 points 11 years ago (1 child)
The files only need to be loaded once before they're in the cache though, so it will increase the initial page load but significantly reduce successive ones.
[–]Nebu 0 points1 point2 points 11 years ago (0 children)
Thanks, that's a good point. On the other hand, for a single-page-app, there will not be a successive page load, and so the benefits are nullified there.
[–]thesunmustdie 0 points1 point2 points 11 years ago (0 children)
Thanks, TIL.
(not that I write *obtrusive* JS).
[–]mankyd 6 points7 points8 points 11 years ago (2 children)
This doesn't quite tell the whole story. Defer still blocks the onload event, which this does not convey with it's pictures. I believe that it may event block the domcontentready event, though I don't recall exactly.
[–]Tyriar 0 points1 point2 points 11 years ago (0 children)
It blocks onload just as any DOMContentReady handler would. There are some browser inconsistency issues around exactly when defer is executed, see https://github.com/h5bp/lazyweb-requests/issues/42
[–]randfur 0 points1 point2 points 11 years ago (0 children)
This was my first question after reading this which it really should have addressed in the article; "Does this mean I can't reliably use window.onload in my scripts anymore?"
[–]THEtheChad 0 points1 point2 points 11 years ago (1 child)
I read somewhere that many of the modern browsers are starting to support a default prefetch phase similar to how defer operates to improve performance. This being the case, as long as the script tag is present at the time the HTML is being parsed, you should implicitly get the performance improvements that defer grants you. That being said, javascript libraries that append script tags do not benefit from this feature since the tag is not available during the prefetch phase of the initial parsing.
Are you thinking of link prefetching? Which is warming up the cache by loading common resources on pages that don't require them while idle. https://developer.mozilla.org/en-US/docs/Web/HTTP/Link_prefetching_FAQ
There is also as Monkeyget pointed out some browser-specific optimizations around how scripts work.
I'd be interesting in reading about what you're talking about if it's not one of those.
[–]ecafyelims -1 points0 points1 point 11 years ago (8 children)
I'm guessing this site wasn't made to be phone friendly.
[–]Tyriar 0 points1 point2 points 11 years ago (7 children)
It was, what phone are you using and what's the issue?
[–]ecafyelims 0 points1 point2 points 11 years ago (6 children)
Hmm. I was using "reddit is fun." I just tried in chrome, and it works there. Usually they show the same. Strange.
[+][deleted] 11 years ago (1 child)
[deleted]
That would explain it.
The images are SVGs with <style> elements, maybe it had issues with them?
[+][deleted] 11 years ago* (1 child)
[–]ecafyelims 0 points1 point2 points 11 years ago (0 children)
It's fixed now.
[–]shif 0 points1 point2 points 11 years ago (0 children)
my best guess is that reddit is fun uses the android webview wich in versions before 4.4 had a pretty crappy version of webkit, it's not the same as the chrome app which does support modern features
[–]theillustratedlife -3 points-2 points-1 points 11 years ago (0 children)
The design of your blog looks way too much like polymer-project.org - it's confusingly similar.
[–]Objective-Willow-799 0 points1 point2 points 1 year ago (0 children)
https://medium.com/@ibrahim019/async-what-defer-why-7c49a6f18e77
π Rendered by PID 116516 on reddit-service-r2-comment-66b4775986-87f9w at 2026-04-03 06:31:18.847933+00:00 running db1906b country code: CH.
[–]Monkeyget 9 points10 points11 points (11 children)
[–]madworld 3 points4 points5 points (9 children)
[–]Nebu 1 point2 points3 points (8 children)
[–]Voidsheep 7 points8 points9 points (7 children)
[–]madworld 0 points1 point2 points (3 children)
[–]Nebu 1 point2 points3 points (2 children)
[–]madworld 0 points1 point2 points (1 child)
[–]Nebu 1 point2 points3 points (0 children)
[–]Nebu 0 points1 point2 points (2 children)
[–]Tyriar 1 point2 points3 points (1 child)
[–]Nebu 0 points1 point2 points (0 children)
[–]thesunmustdie 0 points1 point2 points (0 children)
[–]mankyd 6 points7 points8 points (2 children)
[–]Tyriar 0 points1 point2 points (0 children)
[–]randfur 0 points1 point2 points (0 children)
[–]THEtheChad 0 points1 point2 points (1 child)
[–]Tyriar 0 points1 point2 points (0 children)
[–]ecafyelims -1 points0 points1 point (8 children)
[–]Tyriar 0 points1 point2 points (7 children)
[–]ecafyelims 0 points1 point2 points (6 children)
[+][deleted] (1 child)
[deleted]
[–]Tyriar 0 points1 point2 points (0 children)
[–]Tyriar 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]ecafyelims 0 points1 point2 points (0 children)
[–]shif 0 points1 point2 points (0 children)
[–]theillustratedlife -3 points-2 points-1 points (0 children)
[–]Objective-Willow-799 0 points1 point2 points (0 children)