Latest techniques for On-Demand Javascript, form my presentation at ScaleCamp - London, December 2009.
Covers Script Islands; "defer" and "async" attributes; survey of Javascript loading techniques; library support.
12. script islands
but ... must still handle a closing
“</script>” in the body.
<script type=”x-tiddler”>
alert(“</script>”); // fail!
</script>
13. script islands
alternative technique - HTML comments.
http://googlecode.blogspot.com/2009/09/gmail-for-
mobile-html5-series-reducing.html
<!--
alert(“hello world!”);
-->
... still have to deal with unwanted “-->” in the script
17. async with script tag
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.src = ('https:' == document.location.protocol ?
'https://ssl' : 'http://www') +
'.google-analytics.com/ga.js';
ga.setAttribute('async', 'async');
document.documentElement.firstChild.appendChild(ga);
})();
18. async and defer
<script> no defer defer
blocks until downloaded eval after document
no async and eval’d parsed
eval’d asynchronously, once available (async=true
async overrides defer=false, but should explicitly use
defer=true for legacy support)