定义和用法
opener 属性是一个可读可写的属性,可返回对创建该窗口的 Window 对象的引用。
opener 属性非常有用,创建的窗口可以引用创建它的窗口所定义的属性和函数。
语法
window.opener
提示和注释
注释:只有表示顶层窗口的 Window 对象的 operner 属性才有效,表示框架的 Window 对象的 operner 属性无效。
实例:
下面例子展示了如何打开一个孩子的窗口和打开的窗口和孩子之间的沟通:
父页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>load</title> </head> <body> 先用这个按钮打开一个窗口: <button id="childOpener" onclick="OpenChildWindow ();">打开子窗口!</button> <br /><br /> 您可以更改子窗口的选择列表中按钮的文本颜色。 <script type="text/javascript"> function OpenChildWindow() { window.open ("childWindow.html", "OpenerTest", "width=300, height=200"); } </script> </body> </html>
子页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> 改变按钮的颜色在开启窗口: <select onchange="ChangeOpener (this)"> <option />black <option />red <option />blue <option />green </select> <script type="text/javascript"> function ChangeOpener(selectTag) { var openerWindow = window.opener; if(openerWindow != null){ var button = openerWindow.document.getElementById("childOpener"); button.style.color = selectTag.options[selectTag.selectedIndex].text; } } </script> </body> </html>