我知道有很多类似的问题,但由于无法解决问题,我必须再次提出这个问题,并附上代码。 在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.document.getElementById("iframe0").style.display='none'
。或者使用父子通信 http://davidwalsh.name/window-iframe - TastySpaceApple