TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Script-injected "async scripts" considered harmful

235 pointsby igrigorikabout 11 years ago

19 comments

svmegatronabout 11 years ago
+1 for periodic re-examination of best practices.
评论 #7775850 未加载
评论 #7774313 未加载
mweibelabout 11 years ago
Very nice article indeed.<p>One commonly used variant I missed in the article and I&#x27;d be interested what it&#x27;s positive&#x2F;negative aspects are (if there are differences at all):<p><pre><code> &lt;script&gt; var script = document.createElement(&#x27;script&#x27;); script.src = &quot;http:&#x2F;&#x2F;example.com&#x2F;awesome-widget.js&quot;; script.async = true; document.getElementsByTagName(&#x27;head&#x27;)[0].appendChild(script); &lt;&#x2F;script&gt; </code></pre> (Note the additional async param)
评论 #7778035 未加载
评论 #7777148 未加载
评论 #7778743 未加载
ajtaylorabout 11 years ago
Thank for yet another gem of an article. As a mostly backend developer, I depend a lot on these types of posts to help me keep up with the current front-end best practices. Anything that is on igvita.com I pretty much absorb without questioning due to the consistently high quality (and correctness!) of the posts.
mike-cardwellabout 11 years ago
What if you stick this as the first thing in your body:<p><pre><code> &lt;iframe src=&quot;&#x2F;file.js&quot; style=&quot;display:none&quot;&gt; </code></pre> And then at the very end of the body tag:<p><pre><code> &lt;script src=&quot;&#x2F;file.js&quot;&gt;&lt;&#x2F;script&gt; </code></pre> Wont that allow us to start a non-blocking fetch of the script early, but then execute it later? I assume it wont be fetched twice. Especially if you send future Expires headers.
评论 #7774551 未加载
评论 #7774541 未加载
评论 #7776503 未加载
gue5tabout 11 years ago
What keeps browsers from running the scripts immediately and only blocking if&#x2F;when the script actually does access the CSSOM? It seems like an easy latency win.
评论 #7774284 未加载
Terr_about 11 years ago
I like Dojo&#x2F;AMD because when you specify dependencies, you implicitly give an execution order.<p>Now, that doesn&#x27;t necessarily help for this kind of &quot;top level&quot; code, but wouldn&#x27;t it be cool if you could say: &quot;Load this &lt;script&gt; tag asynchronously, but only run it once the following other &lt;script&gt; tags finish&quot;? (You can give &lt;script&gt; tags HTML IDs just like anything else, right?)
评论 #7774959 未加载
icambronabout 11 years ago
I don&#x27;t understand why the browser treats the script injection case differently than the blocking script tag case, insofar as blocking on CSSOM is concerned. Why doesn&#x27;t the browser download the script right away and then just hold up executing it while the CSSOM finishes? I can&#x27;t think of why it waits to <i>download</i> it.
评论 #7775763 未加载
ssttooabout 11 years ago
Very cool, thanks for revisiting the best practices of yesteryear!<p>Quick test in FF: seems it blocks either way, but at least schedules the fetch in the async=true case <a href="http://i.imgur.com/AmNjhgz.png" rel="nofollow">http:&#x2F;&#x2F;i.imgur.com&#x2F;AmNjhgz.png</a>
ejainabout 11 years ago
I&#x27;m using LAB.js, which lets me declare dependencies between scripts, and then handles sequential or parallel retrieval and execution. Not sure how I could easily replace this as suggested in the article, unless I merge all js into one big file?
评论 #7777944 未加载
thathonkeyabout 11 years ago
Do these page performance optimizations check out across the modern browsers? Chrome and Firefox tend to implement such things differently. Not sure about Safari, IE11, etc.
putlakeabout 11 years ago
Great stuff from Ilya as usual. But this tip is only important when you&#x27;d like Javascript execution to happen as fast as possible. The problem with async is that JS executes as soon as it is downloaded, and that may be before the page has fully loaded or the DOM has been built. For cases where your core content is in HTML and JS is only used for progressive enhancement, won&#x27;t it be better to use defer?<p>Why isn&#x27;t defer more popular?
评论 #7776708 未加载
robocatalmost 11 years ago
The underlying problem is the loading of the CSS is blocking (blocks DOM as well).<p>However, that problem is complex to workaround.<p>Using the async flag doesn&#x27;t fix that problem, and the async flag also means that you need to make sure your scripts can run in any order (which will cause problems unless you are very careful e.g. use a loader that manages dependencies asynchronously).
radio4fanalmost 11 years ago
Another option is to inject the script:<p>A. At onDOMContentLoaded and B. Inside a setTimeout(func, 0) block<p>Then the browser&#x27;s loading indicator will stop before the script has been loaded, executed and all its assets loaded.<p>Useful for social media widgets, etc.
dfabulichabout 11 years ago
I&#x27;d say this post is mildly hypocritical. You&#x27;re using two script-injected async scripts, one from Google Tag Manager and another from Disqus!<p>Which is to say: if it were <i>that</i> harmful, then maybe you could try fixing them yourself and tell us how it goes? It would be great to provide some advice on how to convert a few popular script-injected async scripts from third parties (especially Google Analytics) into non-&quot;harmful&quot; ones.
Kiroabout 11 years ago
What&#x27;s wrong with just putting the script tag before the closing body tag?
评论 #7774450 未加载
评论 #7774325 未加载
drudru11almost 11 years ago
Great - but what if I need to dynamically load some JS?
评论 #7778739 未加载
namelezzabout 11 years ago
Does this mean requirejs being harmful?
评论 #7777584 未加载
skrebbelabout 11 years ago
Meta, but if you&#x27;re going to write an article named &quot;X considered harmful&quot;, then X really has to be as least as harmful as GOTO&#x27;s prevalence was in 1968. This article is about a little thing that&#x27;s current between now and now+3y, and that doesn&#x27;t really hurt anybody when done wrong. Coding a script tag the wrong way is not going to stop the software engineering industry from making fundamental improvements.
评论 #7775544 未加载
评论 #7774712 未加载
评论 #7775705 未加载
trhwayabout 11 years ago
considered by whom? Saying &quot;it is my opinion that ...&quot; of course wouldn&#x27;t sound that important.
评论 #7774205 未加载