Wednesday, July 09, 2008

JavaScript - Close Window and Refresh Parent

I've run into this scenario more than once: I have a button or link that spawns a new window, usually a window with some sort of form in it, like a rating or comment. After the user make his or her rating or comment and submits it, I want the new window to close ad the window underneath to refresh to show the new rating or comment. So here's a JavaScript that will close a new window and also refresh the parent window.

The function:
<script language="JavaScript" type="text/javascript">
function CloseAndRefresh()
{
opener.location.reload(true);
self.close();
}
</script>
You can call the function a couple of ways. You can use a link:
<a href="#" onClick="CloseAndRefresh(); return:false;">Close</a>
Or a button:
< input type="button" value="close" onClick="CloseAndRefresh();"
Or you can make it part of the submit:
<input type="submit" value="submit" onClick="CloseAndRefresh(); return:true;"

2 comments:

Anonymous said...

It works!
thanks a lot.
by hc

Anonymous said...

thank you so much
it is working nice.