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..
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..
Share Improve this question edited Nov 14, 2011 at 12:58 Tushar Ahirrao asked Nov 14, 2011 at 12:42 Tushar AhirraoTushar Ahirrao 13.1k18 gold badges66 silver badges98 bronze badges 6 | Show 1 more comment7 Answers
Reset to default 8From 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.
You might want to try this
window.opener.close()
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);
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';
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();
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!
function closeAll()
{
window.opener.justClose();
}
iframe
inside a parent window? And you want to close the parent window inside theiframe
? – steveyang Commented Nov 14, 2011 at 12:48