Reputation: 13115
I am trying to close parent window from child window using javascript, but its not going to work.
Please share me code if you have any.
My parent html :
<script>
var winr;
var selfw;
function openAn(){
selfw=self.window;
winr=window.open("shell.htm","");
}
function justClose(){
//setTimeout("closeAfterMin()",2000);
winr.close();
}
function closeAfterMin(){
alert("sd");
selfw.close();
}
</script>
<body onload='openAn()' >
<h1>New Web Project Page</h1>
</body>
My Child html :
<script>
function closeAll(){
window.parent.close();
}
</script>
<body>
<div onclick="closeAll();" onunload="window.opener.close();">
Close Parent and self
</div>
</body>
Please help me..
Upvotes: 4
Views: 51400
Reputation: 509
Updated Answer Having a child close a parent is bad coupling. My original answer only worked because my parent window was itself opened via javascript by a grandparent window!
The correct way to do this, to avoid uneccessary coupling, is to have the parent window poll the child, using setInterval
, for the event that triggers the close. In this example that event is the child window closing. I actually use this to refresh part of the page with ajax rather than closing the window, but the principle is the same...
$('#open_child').click(
function(e) {
e.preventDefault();
docs_popup = window.open($('#open_child').attr('href'),'upload_doc');
var timer = setInterval(function() {
if(docs_popup.closed) {
window.close();
}
}, 1000);
return false;
} );
Original Answer There is actually a fairly simple work around for this.
Create a page that does nothing but close itself, for example close.html
:
<!DOCTYPE html>
<html>
<head>
<script>window.close();</script>
</head>
<body>
<p>This window should close automatically.</p>
</body>
</html>
Then in the child window, redirect the opener to the page that closes itself:
window.opener.location.href='close.html';
Upvotes: 1
Reputation: 21
function closeW() {
window.open('javascript:window.open("", "_self", "");
window.close();', '_self');
}
call the function above with a timeout from child page. It works in IE and sometimes in Chrome too.
setTimeout(closeW, 500);
Upvotes: 1
Reputation: 1731
There is a working solution (in Chrome, at least):
Here's the code for parent page:
<html>
<head>
<script type="text/javascript">
function openChild() {
var w = window.open("parent_close_child.html", "child");
}
</script>
</head>
<body>
<input type="button" value="Open" onclick="openChild()" />
</body>
</html>
Here's the code for child page:
<html>
<head>
<script type="text/javascript">
function closeParent() {
var w = window.opener;
window.opener = self;
w.close();
}
</script>
</head>
<body>
<input type="button" value="Close parent" onclick="closeParent()" />
</body>
</html>
Just try it!
Upvotes: -1
Reputation: 32598
From window.close() MDN:
This method is only allowed to be called for windows that were opened by a script using the window.open method. If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script.
You can close the child window from the parent, but not the other way around.
Upvotes: 8
Reputation: 13994
If you mean a child-frame within a parent-frame, then use
window.parent.close();
If you mean an opened window (with window.open()), then try this
window.opener.close();
Upvotes: 0