2

使用 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/

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

4

0 回答 0