如何使用 javascript 删除 iframe 本身

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21881901/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:53:55  来源:igfitidea点击:

How do I remove iframe within itself by using javascript

javascripthtmljsfiframexhtml

提问by séan35

I know that there are several similar questions, but I have to ask the question again with attached code because of being unable to work out. I have two .xhtml file in JSF project. One is mainPage.xhtml has a button that generates dynamic html code to create an iframe (iFramePage.xhtml) and show it on the browser;

我知道有几个类似的问题,但由于无法解决,我不得不用附加的代码再次提出问题。我在 JSF 项目中有两个 .xhtml 文件。一个是 mainPage.xhtml 有一个按钮,可以生成动态 html 代码来创建 iframe(iFramePage.xhtml)并在浏览器上显示;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head>
    <h:outputStylesheet library="css" name="style.css" />

    <script type="text/javascript">

        /** Create dynamic iframe HTML code for iFramePage.xhtml **/
        function createIFrameHTML(){
            document.getElementById("iFrameContainer").innerHTML =  '<div id="iframe0"><iframe src="iFramePage.xhtml" width="450px" height="300px"></iframe></div>';
        }

        /** Close iFrame **/
        function removeElement() {

        /*Both lines work properly when I call inside this page, */
        /*..however it does not work by calling from iFramePage.xhtml */                

        //document.getElementById("iFrameContainer").removeChild("iframe0");
        $('iframe0').remove();
        }
    </script>
</h:head>
<body>
    <f:view>
        <h:form id="mainForm">

            <!-- Control Menu -->
            <div id="cntrMenu">
                <h:commandButton id="cntrBtn1" 
                    onclick="createIFrameHTML();return false;"></h:commandButton>
                <h:commandButton id="cntrBtn2" 
                    onclick="removeElement();return false;"></h:commandButton>  
            </div>

            <div id="iFrameContainer">
                <!-- an iframe will be generated by createIFrameHTML() -->
            </div>
        </h:form>
    </f:view>
</body>
</html>

The other page is iFramePage.xhtml that has some html and javascript code;

另一个页面是 iFramePage.xhtml,它有一些 html 和 javascript 代码;

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <h:outputScript name="......js" />
    <h:outputStylesheet name="....css" />
    <script>

        /** Close iFrame.**/
        function closeSelf() {
            /* Two lines works properly, however third line does not work!*/
            //window.top.location.href = "HIDDEN"; 
            //parent.document.location.href = "HIDDEN";
            parent.removeElement();

        }
    </script>
</h:head>
<body>  
    <input jsfc="h:commandButton" id="exitBtn" value="Kapat" onclick="closeSelf();" />
</body>
</html>

I can generate the iframe by clicking "cntrBtn1" button and removing by clicking "cntrBtn2" inside mainPage.xhtml. However, I need to remove the iframe within itself (iFramePage.xhtml). When I click "exitBtn" in iFramePage.xhtml, the iframe does not disappear. There is nothing about cross-domain, because mainPage.xhtml and iFramePage.xhtml are in the same JSF project, even in the same directory. I can redirect the parent page (looks at two lines in closeSelf() in iFramePage.xhtml), but I cannot remove the iframe by using parent element, why! Please, help me :)

我可以通过单击“cntrBtn1”按钮并通过单击 mainPage.xhtml 中的“cntrBtn2”删除来生成 iframe。但是,我需要删除自身内部的 iframe (iFramePage.xhtml)。当我在 iFramePage.xhtml 中单击“exitBtn”时,iframe 不会消失。跨域没什么好说的,因为mainPage.xhtml和iFramePage.xhtml在同一个JSF项目中,甚至在同一个目录下。我可以重定向父页面(查看 iFramePage.xhtml 中 closeSelf() 中的两行),但是我无法使用父元素删除 iframe,为什么!请帮我 :)

回答by Subin

Communicate between the parent and iframe using window.postMessage.

使用window.postMessage在父级和 iframe 之间进行通信。

Replace the closeSelf()function in iframepage to the following :

iframe页面中的closeSelf()函数替换为以下内容:

function closeSelf() {
   parent.window.postMessage("removetheiframe", "*");
}

and on the parent page, add the following code to listen when the iframesends a message :

并在父页面上,添加以下代码以在iframe发送消息时进行侦听:

function receiveMessage(event){
   if (event.data=="removetheiframe"){
      var element = document.getElementById('iframe-element');
      element.parentNode.removeChild(element);
   }
}
window.addEventListener("message", receiveMessage, false);

You can also check the origin of postMessageby event.originto make sure that the right iframerequested to remove the iframe.

您还可以通过event.origin检查postMessage来源,以确保正确的iframe请求删除iframe