Results 1 to 10 of 10

Thread: removeChild() and IFRAME

  1. #1
    Join Date
    Apr 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question removeChild() and IFRAME

    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:

    Code:
    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):
    Code:
    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)

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    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:
    Code:
    <script type="text/javascript">
    onload=function() {
      document.getElementById("el").removeChild(document.getElementById("el").firstChild);
      }
    </script>
    ...
    <div id="el">
    <iframe ...>
    </div>
    - Mike

  3. #3
    Join Date
    Apr 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    span should work yes

  5. #5
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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...

  6. #6
    Join Date
    Apr 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

    Any ideas?

  7. #7
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    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

  8. #8
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Hiding it will remove the element focus. Use:
    Code:
    style="position:absolute;left:-20000;"
    - Mike

  9. #9
    Join Date
    Apr 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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!

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    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:
    Code:
    document.body.removeChild(document.getElementById("iframeID"));
    It should work.
    - Mike

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •