如何使用JavaScript从iframe中移除自身?

7

我知道有很多类似的问题,但由于无法解决问题,我必须再次提出这个问题,并附上代码。 在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>

另一个页面是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>

我可以通过点击“cntrBtn1”按钮生成iframe,通过在mainPage.xhtml中点击“cntrBtn2”来删除它。但是,我需要在iFramePage.xhtml内部删除iframe。当我在iFramePage.xhtml中点击“exitBtn”时,iframe并没有消失。这与跨域无关,因为mainPage.xhtml和iFramePage.xhtml在同一个JSF项目中,甚至在同一个目录中。我可以重定向父页面(在iFramePage.xhtml的closeSelf()函数中查看两行代码),但我不能使用父元素来删除iframe,请帮助我 :)

“parent” 不访问 “iframe” dom,而是父窗口。您可以尝试 parent.document.getElementById("iframe0").style.display='none'。或者使用父子通信 http://davidwalsh.name/window-iframe - TastySpaceApple
我认为,与其使用“parent.document.getElementById("iframe0").style.display='none'”,更合适的做法是使用“parent.document.getElementById("iFrameContainer").innerHTML = ' '"。谢谢你的建议,它给了我灵感。然而,我无法成功地应用你信息中提到的window.postMessage。 - séan35
1个回答

14

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

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

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

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

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

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


我尝试应用您的解决方案,但无法正确工作。在您的解决方案之后,mainPage.xhtml甚至没有生成iframe,即使单击“cntrBtn1”按钮。 - séan35

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接