Skip to content

Instantly share code, notes, and snippets.

@andykent
Created May 24, 2011 16:46
Show Gist options
  • Save andykent/989092 to your computer and use it in GitHub Desktop.
Save andykent/989092 to your computer and use it in GitHub Desktop.
Simple example showing advantages of async JS loading
alert('Hello from alert.js');
<html>
<head>
<script type="text/javascript">
(function() {
var t = document.createElement('script'); t.type = 'text/javascript'; t.async = true; t.src = "./alert.js";
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(t, s);
})();
</script>
</head>
<body>
<p>You should see me before the alert fires :)</p>
</body>
</html>
<html>
<head>
<script src="./alert.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<p>The alert will fire before you see me and you'll be waiting ages :(</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment