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
3 Answers
Reset to default 3Define 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.
本文标签:
版权声明:本文标题:javascript - How do I remove an iframe from within itself (that has been dynamically created and loaded with src) - Stack Overfl 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744537393a2611397.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论