JavaScript通过window.opener向父窗口输出内容

作者:小括号时间:2026-10-29 08:09:59浏览:72

本文介绍利用JavaScript的window.open方法打开新窗口,并通过子窗口的window.opener属性访问父窗口文档对象,实现向父窗口写入HTML内容的具体代码示例。

JavaScript通过window.opener向父窗口输出内容

本文通过具体代码示例,介绍在JavaScript中打开新窗口后,如何向父窗口输出内容的实现方式。

以下代码演示了使用window.open方法创建弹出窗口,并利用该窗口的引用句柄向父窗口写入信息的过程。


<!DOCTYPE html>
<html>
<head>
<script>
function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.focus();
myWindow.opener.document.write("<p>This is the source window!</p>");
}
</script>
</head>
<body>
<input type="button" value="Open 'myWindow'" onclick="openWin()" />
</body>
</html>

希望本文提供的代码示例对JavaScript开发有所帮助。

作者最新文章