本文介绍利用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开发有所帮助。