更改脚本元素的src属性以避免IE中的内存泄漏不再适用于IE9,IE10

时间:2013-07-15 19:04:10

标签: javascript internet-explorer jsonp

使用IE7和IE8,我发现可以通过向头部添加脚本元素并简单地更改src属性来“避免单个页面应用程序”中的内存泄漏,该“单页面应用程序”正在进行频繁的JSONP调用。每次更改src属性时,它都会立即加载并运行脚本。这不再适用于IE9或IE10。使用JQuery的.ajax()或手动将前一个脚本节点从头部移除,添加一个新的(在FF和Chrome中正常工作)会导致IE中的内存泄漏。

这是我用来提交JSONP的基本代码--Jquery和其他库似乎泄漏内存,我想知道我是否可以完全避免使用ie9和ie10 ...

// Some statics used by JSONP calls (below)... uuid is used to prevent getting stale cached results, it forces a new "get" every time by changing the url
Testing123.uuid = 0;
Testing123.head = document.getElementsByTagName('head')[0];
//-----------------------------------------------------
// mainurl is the url we are going to, callbackFuncName is the callback function, parameters must be a string with zero or more parameters already encoded
//    formatted as "&parm1=value1&parm2=value2" as it is being tacked onto a GET url...
Testing123.debugJSONP = false; // set to true to see stuff in console
Testing123.initiateJSONP = function (mainurl, callbackFuncName, parameters) {
    var url = mainurl + "?callback=" + callbackFuncName + "&uuid=" + (Testing123.uuid++);
    var script;
    url += parameters; // add optional parameters.  
    // Now, let's make the JSONP call happen.  One way for IE 8 and below, another way for FF, Chrome, etc.
    if (Testing123.isIE) { 
        // ***** NOTE  ***** 
        //   This tests for ALL ie versions, but ie9 and ie10 will only display one interation...
        //   If you add && Testing123.browserVersionNumber < 9.0 to the if above, then the iterations will work, but 
        //   memory usage will go up dramatically if run for a while...
        // ***** NOTE ******

        // For IE, we create the script node just once, and then set its src attribute to run again...
        // ***** This seems now to fail in ie9 and ie10
        var addToDOM = 0;
        script = document.getElementById('JSONP');
        if (!script) {
            if (Testing123.debugJSONP) Testing123.logToOutput("initiateJSONP with IE: creating script element with id JSONP");
            script = document.createElement('script');
            script.id = 'JSONP';
            script.type = 'text/javascript';
            script.charset = 'utf-8';
            addToDOM = 1;
        }
        if (Testing123.debugJSONP) Testing123.logToOutput("initiateJSONP with IE: setting script element's src to " + url);
        script.setAttribute("src", url);
        //script.src = url;
        if (addToDOM) // Only do this the first time we create it...
        {
            if (Testing123.debugJSONP) Testing123.logToOutput("initiateJSONP with IE: appending script element with id JSONP to head");
            Testing123.head.appendChild(script);
        }

    } else {
        //First lets clean up the DOM from the last call, if there was one...
        var tmp;
        while (tmp = document.getElementById('JSONP')) {
            if (Testing123.debugJSONP) Testing123.logToOutput("initiateJSONP non IE: found a JSONP element by id... asking parent to remove it and deleting its properties.");
            tmp.parentNode.removeChild(tmp);
            // not working in IE 7/8/9
            for (var prop in tmp) {
                //if (Testing123.debugJSONP) Testing123.logToOutput("initiateJSONP non IE: deleting prop: [" + prop + "] from the element found.");
                delete tmp[prop];
            }
            tmp = null;
        }

有没有人能解决这个问题?这是一个带有一点测试应用程序和所有代码的jsfiddle:

http://jsfiddle.net/bbct/9RqZ6/

提前感谢任何建议/见解。

0 个答案:

没有答案