problem with iframe: can´t access parent document

Discussion in 'JavaScript' started by aleiderman, Aug 27, 2008.

  1. #1
    I have a bookmarklet that opens an frame in any web page. The iFrame is opened from a parent js document, which also has a method to close it up. My problem is that the close method can only be accessed from the window, but not the iframe.

    The question is:
    How do I access from the iframe the js methods of the parent document, to the point, how do close the iframe?
     
    aleiderman, Aug 27, 2008 IP
  2. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Inside the iframe you have a 'parent' object which is the window. This means that if the iframe has an id 'myiframe' you can access the window using:
    document.getElementById('myiframe').parent
    Code (markup):
    I don't know how you wanna 'close' the iframe but i guess removing it will do the job. You can use something like that (presuming it still has the id 'myiframe'):
    document.body.removeChild(document.getElementById('myiframe'));
    Code (markup):
    or the long way to do the same...
    document.getElementById('myiframe').parent.removeChild(document.getElementById('myiframe'));
    Code (markup):
     
    xlcho, Aug 27, 2008 IP