window opener 属性定义
opener 返回对创建此窗口的窗口的引用,该属性是一个可读写的属性。创建的窗口可以引用父窗口所定义的属性和函数。语法如下:
window.opener
提示:只有表示顶层窗口的 window 对象的 operner 属性才有效,表示框架的 window 对象的 operner 属性无效。
opener 属性实例
下面的例子将弹出窗口的输入值传递给父窗口显示出来。
1.html:
<html>
<body>
<script type="text/javascript">
myWindow=window.open('2.html','MyName','width=300,height=200')
</script>
</body>
</html>
2.html:
<html>
<body>
<script type="text/javascript">
function getInput(){
var input_char = document.getElementById("input_char").value;
window.opener.document.write(input_char);
}
</script>
</body>
<input id="input_char" type="text" value="输入些文字" />
<button onclick="getInput()">点击我试试</button>
</html>
运行1.html(设置允许浏览器弹出窗口),在弹出窗口的文字输入框输入文字,点击按钮,1.html 页面会打印出输入的文字。
参考阅读
- JavaScript getElementById() 方法:根据给定的 id 查找文档元素
