admin管理员组

文章数量:1384726

I want to be able to remove an iframe from within itself. The iframe is created dynamically and the content is loaded with 'src'.

I create my iframe like this:

    var i = document.createElement('iframe');
    i.id = 'proxy_frame';
    i.name = 'proxy_frame';
    i.setAttribute('src', url);
    document.body.appendChild(i);

Then from within 'url' I want to be able to remove/close the iframe.

Before loading the data into the iframe with src I used document.write:

    window.frames['proxy_frame'].document.write(html);

and then I was abloe to remove the iframe with:

    window.parent.document.getElementById("proxy_frame").parentNode.removeChild(window.parent.document.getElementById("proxy_frame"));

But this does not work with 'src'.

Note: This is for a bookmarklet so I don't want to use jQuery or another library.

I want to be able to remove an iframe from within itself. The iframe is created dynamically and the content is loaded with 'src'.

I create my iframe like this:

    var i = document.createElement('iframe');
    i.id = 'proxy_frame';
    i.name = 'proxy_frame';
    i.setAttribute('src', url);
    document.body.appendChild(i);

Then from within 'url' I want to be able to remove/close the iframe.

Before loading the data into the iframe with src I used document.write:

    window.frames['proxy_frame'].document.write(html);

and then I was abloe to remove the iframe with:

    window.parent.document.getElementById("proxy_frame").parentNode.removeChild(window.parent.document.getElementById("proxy_frame"));

But this does not work with 'src'.

Note: This is for a bookmarklet so I don't want to use jQuery or another library.

Share Improve this question asked May 22, 2011 at 9:07 Andreas ThorstenssonAndreas Thorstensson 752 silver badges2 bronze badges 4
  • Is the url on the same domain? – pimvdb Commented May 22, 2011 at 9:13
  • Do you mean when you set the src property it no longer is able to be deleted? That sounds like you may be hitting Same Origin Policy. – alex Commented May 22, 2011 at 9:15
  • Url is not on the same domain. So of course its same origin policy, which does not happen if I load the content with document.write(). Thanks. I wonder if I can solve this some other way though. – Andreas Thorstensson Commented May 22, 2011 at 9:45
  • And since I do not want to use any libraries json-p really isnt an option. – Andreas Thorstensson Commented May 22, 2011 at 9:52
Add a ment  | 

3 Answers 3

Reset to default 3

Define a method in your parent page

function removeElement() {
  var d = document.getElementById('body'); // or any other parent element
  var proxy_frame = document.getElementById('proxy_frame');
  d.removeChild(proxy_frame);
}

To call this method from your iframe simply use this

<a href="#" onclick="top.window.removeElement();">Remove me</a>

For local domain iframes you don't need to rely on ids.

window.parent.document.body.removeChild(window.frameElement);

window.frameElement has reasonable support https://developer.mozilla/en-US/docs/Web/API/Window/frameElement

You can't access the parent page as long as it's in a different domain.

Set up a page in your site that can be used to remove the iframe, then in the iframe you just go to that page.

本文标签: