.利用Script标签可以跨域加载并运行一段JavaScript脚本, 但Neil Fraser先前已指出,脚本运行后资源并没被释放,即使是Script标签移除后。为了释放脚本资源,通常在返回后还要一些进行额外的处理
 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';
  // 标签加到head后,会自动加载并运行。
  var head = document.getElementsByTagName('head')[0];
  head.appendChild(script)
实际上很多流行的JS库都采用这种方式,创建一个scritp标签,赋予一个ID后加载脚本(比如YUI get()),加载完并回调后清除该标签。问题在于当你清除这些script标签的时候,浏览器仅仅是移除该标签结点。
var script = document.getElementById('JSONP');
script.parentNode.removeChild(script);
当浏览器移除这标签结点后的同时并没对结点内JavaScript资源的进行垃圾回收,这意味着移除标签结点还不够,还得手动的清除script标签结点的内容:
// Remove any old script tags.
  var script;
  while (script = document.getElementById('JSONP')) {
    script.parentNode.removeChild(script);
    // 浏览器不会回收这些属性所指向的对象.
    //手动删除它以免内存泄漏.
    for (var prop in script) {
      delete script[prop];
    }
  }
问题来了:::--------》ie中没有script[prop]不支持。那么在IE中如何手动删除。以免内存泄露

解决方案 »

  1.   

    你如何知道内存泄露的?我只听说JS的闭包和COM调用有内存泄露。
      

  2.   

    原文出处:http://ajaxian.com/archives/dynamic-script-generation-and-memory-leaks译文:http://www.bgscript.com/archives/410
      

  3.   

    问题来了:::--------》ie中没有script[prop]不支持。那么在IE中如何手动删除。以免内存泄露
    ==================================================================================看了文章.里面说了.如上的代码会导致所有的浏览器泄漏.
    可以通过delete attribute去删除所有属性但除了IE浏览器.因为IE不允许delete删除对象的
    原生属性.
    //=========== 引用他回复的一段话================================
    @Spocke and others, this is not specific to IE. All browsers (IE, Firefox, Safari, Opera, Chrome) leak memory with this issue. Deleting the properties on the script tag eliminates this leak in all browsers except IE, as IE doesn’t let you delete native properties from DOM nodes. However in IE (but not in any other browser) one can reuse the script tag simply by resetting the SRC attribute. This important detail is mentioned in my article, but not in Ajaxian’s summary
    //==============================================================
    这里提到了can reuse the script tag simply by resetting the SRC attribute在IE可以很容易的通过重置src属性来重新使用script标签
      

  4.   

    你的意思是先设一个全局的script.再设置里面的值var script = document.createElement('script'); //创建标签
    function postCall(url) {
       
        script.src = url;
        script.id = 'JSONP';
        script.type = 'text/javascript';
        script.charset = 'utf-8';
        // 标签加到head后,会自动加载并运行。
        var head = document.getElementsByTagName('head')[0]; 
        head.appendChild(script);
      }这样就通过调用postCall(url)来设置src,这样原来的src所指向的内容就会被释放掉。
      

  5.   

    不管通过什么方式 只要你重新设置src那么就是新的script