【发布时间】:2013-09-10 11:58:12
【问题描述】:
一页使用window.open()方法打开另一页(例如,OpenerDemo.html),但Popup页面无法访问Opener页面的任何属性。
开启页面代码:
<head>
<meta http-equiv="content-type" content="html/text"; charset="utf-8" >
<title>windowdemo</title>
<script language="JavaScript">
function openWin(thisurl) {
popWin = window.open(thisurl, 'popupPage', "width=480,height=272");
}
</script>
</head>
<body>
<input type="button" value="open" onClick="openWin('openerdemo.htm')"/>
</body>
弹窗(openerdemo.htm)代码:
<html>
<head>
<meta http-equiv="content-type" content="html/text"; charset="utf-8" >
<title>windowdemo</title>
<script language="JavaScript">
function closeWin() {
window.opener.close();
window.close();
}
</script>
</head>
<body>
<h1><a href="#" onClick="closeWin()">close all</a></h1>
</body>
我在Chrome中使用javascript控制台,在弹出窗口的cmd行输入'window.opener',返回:
window.opener
'窗口 {}',
这意味着打开窗口不为空,但它的所有属性都丢失了。但是,如果一个页面像这样打开一个新页面:
popWin = window.open('', 'popupPage', "width=480,height=272");
popWin.document.write("这是popupPage");
弹出页面的window.opener是opener窗口的ref,只是可以通过'window.opener'对象来控制opener窗口。 例如:
<body>
<script type="text/javascript">
myWindow=window.open('','','width=200,height=100')
myWindow.document.write("This is 'myWindow'")
myWindow.focus()
myWindow.opener.document.write("This is the parent window")
</script>
</body>
我在 FF、IE 和 chrome 中测试了这段代码。
谁能告诉我如何控制弹出页面中的开启器窗口?
【问题讨论】:
标签: javascript html