Dynamic script generation and memory leaks


An interesting piece by Neil Frasher shows that using JSON-P with generant script nodes can be quite a memory leak.Normally you'd information returned from API in JSON-P with a generanted scripted nopte
JAVASCRIPT: 
script = document.createElement('script');
script.src = 'http://example.com/cgi-bin/jsonp?q=What+is+the+meaning+of+life%3F';
script.id = 'JSONP';
script.type = 'text/javascript';
script.charset = 'utf-8';
// Add the script to the head, upon which it will load and execute.
var head = document.getElementsByTagName('head')[0];
head.appendChild(script) 
 
The issue there is that you clog up the head of the document with lots of script nodes,which is why most libries(like the YUI get)(utility)will add an ID to the script element and remove the node after Jfter
JAVASCRIPT: 

var script = document.getElementById('JSONP');
script.parentNode.removeChild(script);
 
 
The issue with this that browsers do remove the node but fail to do a clean garbage collection of the JavaScript the same time.This means to cleanly remove the script and its content,you need to prodefile of
JAVASCRIPT: 

// Remove any old script tags.
  var script;
  while (script = document.getElementById('JSONP')) {
    script.parentNode.removeChild(script);
    // Browsers won't garbage collect this object.
   // So castrate it to avoid a major memory leak.
    for (var prop in script) {
      delete script[prop];
    }
  }
 
from:http://ajaxian.com/archives/dynamic-script-generation-and-memory-leaks