PDA

View Full Version : removeChild() and IFRAME



phb5000
04-10-2007, 01:57 PM
Hi,

I'm attempting to dynamically remove an IFRAME using the DOM method "removeChild()". However, when I try doing this JavaScript throws the following error/expcetion:


Error: uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMWindowInternal.alert]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: http://.../general.js :: deleteIframe :: line 183" data: no]


On the following line(s):


var doc = window.parent.document;
var ifr = doc.getElementById('myIframe');
ifr.parentNode.removeChild(ifr);


Even though javascript throws the exception the IFRAME does get removed, however, script execution completely stops after it (which is a big problem).

What is causing the exception?
Can i catch the exception?
Is there any other way of removing an IFRAME dynamically?

(Running Mozilla Firefox 1.5.0.11)

mburt
04-10-2007, 02:05 PM
Iframes (ifr) can't have document/window attributes. It would be an "access denied" type error.
To remove the iframe, place in a div, and use the same method:

<script type="text/javascript">
onload=function() {
document.getElementById("el").removeChild(document.getElementById("el").firstChild);
}
</script>
...
<div id="el">
<iframe ...>
</div>

phb5000
04-10-2007, 02:25 PM
Thanks for your reply, however, placing the iframe inside a div is not something I can do.

The only alternative method I've found so far is by simply hiding the iframe "style.display = 'none'". However, this would eventually cause my site to "overflow".

Is there anyway of catching the above exception so that I can continue script execution after the exception has been thrown?

Is there anything else (instead of a DIV) I could place my IFRAME in so that I can remove it. I cannot put my IFRAME inside a div since my IFRAMEs are movable and overlappable. This would cause issues for my solution.

Would for example a SPAN work?

boogyman
04-10-2007, 02:45 PM
span should work yes

pcbrainbuster
04-10-2007, 03:24 PM
Makke sure you use position: absolute in your iframe's style attribute... That makes it layer over the document like paper on paper and should not take up more space/or cause an overflow...

phb5000
04-10-2007, 04:45 PM
I just tried using my same javascript code for removing a child node with the IFRAME enclosed in a SPAN.

I got the same exception/error :confused:

Any ideas?

boogyman
04-10-2007, 04:59 PM
how about you try style="visibility:hidden"

that will keep the iframe in the same position, however it would make it not appear, so you shouldn't run into the problem you do with display: none

mburt
04-10-2007, 08:11 PM
Hiding it will remove the element focus. Use:

style="position:absolute;left:-20000;"

phb5000
04-11-2007, 02:23 PM
Thanks, I think I'll just have to go with hiding the iframes when im done with them.

I'm guessing there is no "safe" way of actually deleting them from the content of my document.

Thanks for the help!

mburt
04-11-2007, 03:43 PM
I'm guessing there is no "safe" way of actually deleting them from the content of my document.

There is, you just have to do it right.
For example, document.body has the removeChild(); function as well as any other object. So if you did:

document.body.removeChild(document.getElementById("iframeID"));
It should work.