有时我们需要在父窗口和子窗口中进行交互,比如在子窗口中上传完文件,将上传的结果返回到父窗口中。本文介绍的就是一个简单的交互实例。如果你的头脑够灵活,完全可以举一反三。
<script type="text/javascript">
<!--
function openWindow()
{
newWindow = window.open('','newWindow','height=300,width=300,scrollbars=auto');
if (newWindow != null)
{
var windowHTML= "<html><head><title>preview</title></head>";
windowHTML += "<body><h1 align='center'>";
windowHTML += "这是子窗口!</h1><hr><div align='center'><form action='#' method='get'>";
windowHTML += "<input type='button' value='将父窗口的背景设为红色' onclick='window.opener.document.body.style.backgroundColor=\"red\";' /><br>";
windowHTML += "<br ><input type='button' value='关闭' onclick='self.close();' />";
windowHTML += "</form></div></body></html>";
newWindow.document.write(windowHTML);
newWindow.focus();
}
}
//-->
</script>
<input value='打开子窗口' onclick="openWindow();" type="button">
<input type="button" value="将子窗口的背景设为蓝色" onclick="if (window.newWindow){newWindow.document.bgColor='blue';newWindow.focus();}" />