Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unloading/Removing content from an iFrame

Is there anyway to unload a page that has been loaded inside an iframe? I do not want to change the iframe src to a blank page if possible. I am basically looking for something that will do something like this $('#frameID').attr("src",""); except that code does not seem to clear the previously loaded page.

Is there a "unload" function that I can call which will reset the iframe so that it does not have any content loaded inside?

like image 364
Dan Avatar asked Aug 20 '09 15:08

Dan


2 Answers

The other solutions use innerHTML, which won't always work in XHTML. They also only clear document.body (anything in the <head> is still present). Here is a solution that uses the DOM:

var frame = document.getElementById("myFrame"), frameDoc = frame.contentDocument || frame.contentWindow.document; frameDoc.removeChild(frameDoc.documentElement); 

This solution uses innerHTML:

var frame = document.getElementById("myFrame"), frameDoc = frame.contentDocument || frame.contentWindow.document; frameDoc.documentElement.innerHTML = ""; 
like image 101
Eli Grey Avatar answered Oct 03 '22 10:10

Eli Grey


Try this,

$("iframe").contents().find("body").html(''); 

It only clears innerHTML of your tag inside and not actually unload your iframe so you can reuse your iframe without reloading it and its working in all browsers and quite simple!!

like image 24
mayurk Avatar answered Oct 03 '22 09:10

mayurk