Javascript 更改脚本元素';避免IE内存泄漏的src属性在IE9、IE10中不再有效

Javascript 更改脚本元素';避免IE内存泄漏的src属性在IE9、IE10中不再有效,javascript,internet-explorer,jsonp,Javascript,Internet Explorer,Jsonp,在IE7和IE8中,我发现通过向头部添加脚本元素并简单地更改src属性,可以避免频繁执行JSONP调用的“单页应用”中的内存泄漏。每次更改src属性时,它都会立即加载并运行脚本。这在IE9或IE10中不再有效。使用JQuery的.ajax()或手动删除头部的上一个脚本节点,并添加一个新的脚本节点(在FF和Chrome中运行良好)会导致IE内存泄漏 这里是我用来提交JSONP-Jquery和其他库的基本代码,我想知道我是否可以用ie9和ie10来避免它 // Some statics used b

在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;
        }
//JSONP调用使用的一些静态(如下)。。。uuid用于防止获取过时的缓存结果,它通过更改url每次强制执行一个新的“get”
Testing123.uuid=0;
Testing123.head=document.getElementsByTagName('head')[0];
//-----------------------------------------------------
//mainurl是我们要访问的url,callbackFuncName是回调函数,参数必须是一个字符串,其中包含零个或多个已编码的参数
//格式为“&parm1=value1&parm2=value2”,因为它被固定到GET url上。。。
Testing123.debugJSONP=false;//设置为true可在控制台中查看内容
Testing123.initiateJSONP=函数(mainurl、callbackFuncName、参数){
var url=mainurl+“?callback=“+callbackFuncName+”&uuid=“+(Testing123.uuid++”);
var脚本;
url+=参数;//添加可选参数。
//现在,让我们实现JSONP调用。一种方法用于IE8及以下版本,另一种方法用于FF、Chrome等。
如果(Testing123.isIE){
//****注****
//这将测试所有ie版本,但ie9和ie10将只显示一个交互。。。
//如果将&Testing123.browserVersionNumber<9.0添加到上面的If中,那么迭代将有效,但是
//如果运行一段时间,内存使用将急剧增加。。。
//******注******
//对于IE,我们只创建一次脚本节点,然后将其src属性设置为再次运行。。。
//******这在ie9和ie10中似乎失败了
var addToDOM=0;
script=document.getElementById('JSONP');
如果(!脚本){
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(“使用IE启动JSONP:将脚本元素的src设置为“+url”);
setAttribute(“src”,url);
//script.src=url;
if(addToDOM)//仅在第一次创建时执行此操作。。。
{
if(Testing123.debugJSONP)Testing123.logToOutput(“initiateJSONP with IE:appending script element with id JSONP to head”);
Testing123.head.appendChild(脚本);
}
}否则{
//首先,让我们从上次调用中清理DOM,如果有。。。
var-tmp;
while(tmp=document.getElementById('JSONP')){
if(Testing123.debugJSONP)Testing123.logToOutput(“initiateJSONP非IE:通过id找到一个JSONP元素…请求父元素删除它并删除它的属性”);
tmp.parentNode.removeChild(tmp);
//不在IE 7/8/9中工作
用于(tmp中的var prop){
//if(Testing123.debugJSONP)Testing123.logToOutput(“initiateJSONP非IE:从找到的元素中删除prop:[“+prop+””);
删除tmp[prop];
}
tmp=null;
}
有人能解决这个问题吗?下面是一个JSFIDLE,其中包含一个小测试应用程序和所有代码:


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

Hmmm,我可能发布得有点早(尽管我发布的代码可能对其他人有用…)我现在正在运行一个15分钟的测试,基于fiddle代码,但是这次没有UI更新,没有控制台日志,没有调试器打开,还有IE9。更改了代码,所以IE9被视为与非IE浏览器一样。提交费用实际上在头几分钟就下降了。提交计数不断上升,然后又下降,我会这样做ndicate垃圾收集定期释放内存。我想我实际上展示了JSONP是好的,这是我需要查看的另一个代码。