Javascript 中父窗口与子窗口的交互

有时我们需要在父窗口和子窗口中进行交互,比如在子窗口中上传完文件,将上传的结果返回到父窗口中。本文介绍的就是一个简单的交互实例。如果你的头脑够灵活,完全可以举一反三。

Javascript 中父窗口与子窗口的交互 – 示例

<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();}" />

关于 Artlover

有多年 web 开发经验,擅长领域 PHP / MySQL / CSS / Javascript / Zend Framework ,期望:在分享中共同成长。
此条目发表在 Javascript 分类目录,贴了 , , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*


*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>