javascript - 如何使用 javascript 在自身中删除 iframe

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

最佳答案

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

关于javascript - 如何使用 javascript 在自身中删除 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21881901/

相关文章:

JSF渲染阶段(为什么执行我的代码?)

javascript - 如何在 Windows 应用商店应用中加载 CDN JS 文件?

php - 无法让按钮附加到文本区域

javascript - 没有 jQuery 的纯 CSS 工具提示

javascript - 多页表单javascript提交

java - 我如何在 Bean 中获取 SessionScope 对象

java - JSF 中渲染错误以及读取适当的 bean 属性时出错

javascript - 无法读取未定义错误的属性 'getState'

javascript - typescript 中的kineticjs形状

javascript - jQuery UI Dialog、Checkboxes和IE6的神秘案例