Tuesday, May 12, 2009

Non-blocking JavaScript

Include via DOM

var js = document.createElement('script'); 
js.src = 'myscript.js'; 
var h = document.getElementsByTagName('head')[0]; 
h.appendChild(js);

Non-blocking JavaScript
  And what about my inline scripts?
  Setup a collection (registry) of inline scripts

Step 1
Inline in the <head>:
var myapp = { 
   stuff: [] 
}; 

Step 2
  Add to the registry
Instead of:
  alert('boo!');
Do:
  myapp.stuff.push(function(){ 
    alert('boo!'); 
  );

Step 3
  Execute all

var l = myapp.stuff.length;  
var l = myapp.stuff.length;  
for(var i = 0, i < l; i++) { 
myapp.stuff[i](); 

No comments :