简体   繁体   English

正确关闭javascript中的iframe,不要隐藏iframe

[英]Closing the iframe from javascript properly and by not hiding the iframe

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. 我想关闭一个iframe ...并且我使用了类似的代码,例如当我单击关闭btn时使iframe可见为false或显示为无...但是由于iframe没有关闭,这正在影响我项目中的其他代码而且我只是隐藏...而且代码可以在Mozilla和Chrome中正常工作,因此可以很好地隐藏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 : 但是在IE中这根本不起作用...所以我尝试了其他一些其他方法,例如...在ifame页面中,在关闭btn时调用此功能,请点击:

    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不会仅打开...但是此显示没有或可见的错误正在影响项目中的其他代码,因此我尝试使用removechild代码再次关闭iframe IE问题...所以您能以正确的代码帮助我关闭iframe ...当我尝试查找代码时(如单击Esc或在iframe之外的任何地方,它(iframe)都能正确关闭,所以我想知道..当我们按Esc或单击iframe之外时,iframe会完美关闭而不影响其他代码时会发生什么。

Thanking You in advance!! 预先感谢您!

you don't 'close' an iframe - you hide it or remove it. 您不会“关闭” iframe,而是将其隐藏或删除。 since hiding it is not an option for you - you should remove it. 因为隐藏它不是您的选择-您应将其删除。 this works in both chrome and IE. 这适用于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的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM