如何使用javascript删除自身内的iframe

时间:2014-02-19 13:27:42

标签: javascript html jsf iframe xhtml

我知道有几个类似的问题,但由于无法解决问题,我不得不再次使用附加代码提出问题。 我在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”进行删除。但是,我需要删除内部的iframe(iFramePage.xhtml)。当我点击iFramePage.xhtml中的“exitBtn”时,iframe不会消失。没有关于跨域的内容,因为mainPage.xhtml和iFramePage.xhtml在同一个JSF项目中,即使在同一目录中也是如此。我可以重定向父页面(在iFramePage.xhtml中查看closeSelf()中的两行),但我无法使用父元素删除iframe,为什么!请帮帮我:)

1 个答案:

答案 0 :(得分:11)

使用 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