Reputation: 25
One page open another page(eg. openerdemo.html) using the window.open() method, but the popup page cannot access any property of the opener page.
Opener page code:
<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>
Popup page(openerdemo.htm) code:
<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>
I use javascript console in Chrome, enter 'window.opener' to the cmd line of popup window, it's return:
window.opener
'Window {}',
That means the opener window is not null, but its all property missing. However, if one page opens a new page like this:
popWin = window.open('', 'popupPage', "width=480,height=272");
popWin.document.write("this is popupPage");
The popup page's window.opener is ref to the opener window, and just can control the opener window with the 'window.opener' object. ex:
<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>
I test this code in FF, IE and chrome.
Can anyone tell me how to control the opener window in popup page?
Upvotes: 1
Views: 6668
Reputation: 55663
It would work fine, but you're making a cross-domain request. If the window you are opening and the window you opened it from are on the same domain, you wouldn't have a problem
Note: This may be classified as a cross-domain request when you are not using a webserver, but just using the file system (file:/// is your protocol). I haven't tested it - rest assured, however, that when you get it on the web, all will be well as long as both the opener and the openee are server from the same domain.
EDIT
I just did a quick test on my local file system and this is indeed the case - it's classified as a cross-domain request and is forbidden for security purposes - again, it won't be a problem when you put it on a webserver and serve both pages from the same domain.
Upvotes: 2