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?
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 = "";
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!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With