HTML DOM 窗口 opener 属性
HTML DOM Window opener 属性返回一个对使用 open() 启动/创建子窗口的父窗口的引用。
语法
以下为语法 −
返回父窗口的引用
window.opener
实例
让我们看一个 HTML DOM Window opener 属性的实例 −
<!DOCTYPE html> <html> <head> <title>HTML DOM Window opener</title> <style> * { padding: 2px; margin:5px; } form { width:70%; margin: 0 auto; text-align: center; } input[type="button"] { border-radius: 10px; </style> </head> <body> <form> <fieldset> <legend>HTML-DOM-Window-opener</legend> <input id="urlSelect" type="url" placeholder="Type URL here..."><br> <input type="button" value="Go To" onclick="openWindow()"> <input type="button" value="Close" onclick="closeWindow()"> <input type="button" value="Restore" onclick="restoreWindow()"> <div id="divDisplay"></div> </fieldset> </form> <script> var urlSelect = document.getElementById("urlSelect"); var winSource; function openWindow() { browseWindow = window.open(urlSelect.value, "browseWindow", "width=400, height=200"); winSource = urlSelect.value; browseWindow.opener.document.getElementById("divDisplay").textContent = "Child Window Active"; } function closeWindow(){ if(browseWindow){ browseWindow.close(); browseWindow.opener.document.getElementById("divDisplay").textContent = "Child Window Closed"; } } function restoreWindow(){ if(browseWindow.closed){ browseWindow = window.open(winSource, "browseWindow", "width=400, height=200"); browseWindow.opener.document.getElementById("divDisplay").textContent = "Child Window Restored"; } } </script> </body> </html>
输出
点击已设置 URL 字段的“转到”按钮 −
点击“关闭”按钮 −
点击“还原”按钮 −
广告