javascript 从javascript正确关闭iframe并且不隐藏iframe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13209343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Closing the iframe from javascript properly and by not hiding the iframe
提问by Shruthi
I want to close an iframe... and i have used the code like making the iframe visible false or as display to none when i click on close btn... but that is effecting other codes in my project as the iframe is not closed and i am just hiding... and also that code works in Mozilla and Chrome fine this concept of hiding the iframe. But in IE this does not work at all... so i tried few other methods like... in the ifame page called this function on close btn click :
我想关闭一个 iframe ......我已经使用了这样的代码,比如当我点击关闭 btn 时,使 iframe 可见为假或显示为无......但这会影响我项目中的其他代码,因为 iframe 没有关闭而且我只是在隐藏...而且该代码在 Mozilla 和 Chrome 中也能正常工作,这很好地隐藏了 iframe 的概念。但是在 IE 中,这根本不起作用......所以我尝试了一些其他方法,例如......在关闭 btn 单击时调用此函数的 ifame 页面:
function closeiframecommt() {
parent.closeiframefriends();
}
and in the page containing iframe i wrote code as:
在包含 iframe 的页面中,我将代码编写为:
function closeiframefriends()
{
var friends = document.getElementById("IFriends");
friends.style.display = "none";
}
so... this works fine in Mozilla and Chrome but in IE... the first time i close the iframe it closes but the next time i try to open iframe it gives error and iframe does not open only... but this display none or visible false is effecting other code in project so i tried removechild code to close iframe again IE issue... so can u please help me with proper code to close iframe... as i tried finding code as when we click Esc or anywhere outside iframe it(iframe) closes properly so i want to know.. what happens when we press Esc or click outside iframe that iframe closes perfectly without effecting other codes.
所以......这在 Mozilla 和 Chrome 中工作正常,但在 IE 中......我第一次关闭 iframe 时它会关闭,但下次我尝试打开 iframe 时它会出现错误并且 iframe 不只打开......但是这个显示none 或visible false 正在影响项目中的其他代码,所以我尝试removechild 代码再次关闭iframe IE 问题...所以你能帮我提供正确的代码来关闭iframe...因为我尝试查找代码,就像我们单击Esc 或iframe 之外的任何地方它(iframe)都会正确关闭,所以我想知道..当我们按 Esc 或单击 iframe 外部 iframe 完美关闭而不影响其他代码时会发生什么。
Thanking You in advance!!
提前谢谢你!!
回答by Mike Corcoran
you don't 'close' an iframe - you hide it or remove it. since hiding it is not an option for you - you should remove it. this works in both chrome and IE.
您不会“关闭” iframe - 您将其隐藏或删除。因为隐藏它不是你的选择 - 你应该删除它。这适用于 chrome 和 IE。
<!DOCTYPE html>
<head>
<title>test</title>
<style type="text/css">
.top {
height:100px;
width:200px;
background-color:green;
}
</style>
<script type="text/javascript">
function removeIFrame() {
var frame = document.getElementById("target");
frame.parentNode.removeChild(frame);
}
</script>
</head>
<body>
<div class="top" onclick="removeIFrame();"></div>
<iframe id="target" src="http://www.disney.com" width="100" height="100"></iframe>
<div class="top"></div>
</body>
the onclick of the top green div handles removing the iframe from the DOM.
顶部绿色 div 的 onclick 处理从 DOM 中删除 iframe。