[ACCEPTED]-Iframes and memory management in Javascript-iframe

Accepted answer
Score: 20

In the iframe, trigger a reload before removing 3 it and then remove it.

<a href="#">Remove</a>
<iframe src="url" />​

$('a').click(function(){
    $('iframe')[0].contentWindow.location.reload();
    setTimeout(function(){
       $('iframe').remove();
    }, 1000);
});​

DEMO here.

Addionally, you can 2 do a manual cleaning up too - i.e. if you 1 have data in your cookies or HTML5 localStorage.

window.onbeforeunload = function(){
    $(document).unbind().die();    //remove listeners on document
    $(document).find('*').unbind().die(); //remove listeners on all nodes
    //clean up cookies
    /remove items from localStorage
}
Score: 2

If any objects from the iframe is referenced 4 in a object in the main window that object 3 won't be removed from the DOM, so, if you 2 have something like this:

Main window:

var object = {};
function iframe_call(data){
    object.iframe_data = data.something
}

iframe:

function onClick(){
    parent_object.iframe_call(this);
}

this 1 happens especially if you refer DOM objects.

Score: 0
var frame = document.getElementById("myframe");
frame.src = "about:blank";

This worked from me and prevented memory 3 leaks. Ig you must destroy the parent of 2 the iframe, do it with some delay to prevent 1 memory leak

More Related questions