Skip to content

Instantly share code, notes, and snippets.

@jakearchibald
Created June 2, 2011 09:40
Show Gist options
  • Save jakearchibald/1004177 to your computer and use it in GitHub Desktop.
Save jakearchibald/1004177 to your computer and use it in GitHub Desktop.
Twitter widgets
(function(document){
var script = 'script',
twitterWidgets = document.createElement(script),
lastScript = document.getElementsByTagName(script)[0];
twitterWidgets.async = twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
lastScript.parentNode.insertBefore(twitterWidgets, lastScript);
})(document);
// compressed 177 bytes
(function(a){var b="script",c=a.createElement(b),d=a.getElementsByTagName(b)[0];c.async=c.src="http://platform.twitter.com/widgets.js",d.parentNode.insertBefore(c,d)})(document)
// insertAfter might work too, saving another byte, but haven't tested
@getify
Copy link

getify commented Jun 2, 2011

@miketaylr-
I've had mixed success getting some versions of Opera to respect it, so I generally assume that it's not reliable there. Then again, Opera seems to be one of those browsers where supporting only the latest version is the only thing that really matters (to most people). And, I know Opera is working on implementing the spec (soonish!?) so I'm ok with a tiny perf degradation temporarily, rather than baking legacy code into my released script projects.

The question is not whether the few bytes matters, it's the question of principle, of not just blindly performing some action in code that has nowhere near as much effect as it appears in code that it does. I bet most developers don't realize the reality I tried to illustrate above. Especially doing so without a comment, leaves the code looking like it's more critical than it is, which trips up later developers (or your future self if you forget).

Remember, in these snippets (like GA snippet and Twitter button snippets, etc), they end up getting thrown into pages and left there untouched for years (or "forever"), so I think it's important to have a cautious eye to what you put in them, not for the bytes but for the responsibility of future-proof and future-thinking code.

I was simply trying to raise awareness about aysnc's actual behavior now, and the rapidly shrinking effectiveness/validity of setting it.

@miketaylr
Copy link

Fair points, Kyle.

@mathiasbynens
Copy link

@miketaylr, do you happen to have more details on when the current version(s) of Opera actually respect .async=true?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment