Javascript Close Popup and Refresh or Redirect Parent Window with OnLoad (Solution)

This one about drove me crazy all night trying to think of why this does not work and to be honest I still don\'t understand why it doesn\'t. But below is a solution to the problem.

The problem was I was trying to close a popup and force the parent page to load a URL using onload.

So within the popup, I saved the form changes to the database and if everything was saved fine it would return to the popup with the code below.

<body onload=\"window.opener.location = \'/directory/?action=confirmation\'; window.close();\">

But for some reason this would cause IE7 (didn\'t check any version below 7 and worked fine in Firefox & Chrome) to just load and load and load but never actually load the page it was sent to. The big blue \"e\" would just spin. The URL was correct and it wasn\'t stuck in some endless loop. But like I said, I\'m still not sure what caused it to do that, but below is the solution that seemed to have worked.

Rather than placing the close and redirect in the onload, I simply added them within a script tag and placed it above all the other scripts.

<script>
   window.opener.location = \'/directory/?action=confirmation\';
   window.close();
</script>

My only guess is that by putting the code in the onload was causing it to not fully run the redirect. Anyway, I hope this helps someone else out there.

Please let me know if you know the reason what caused this error.


comments powered by Disqus