How to refresh parent page after closing popup window in javascript?

我与影子孤独终老i 提交于 2019-12-03 04:58:13

As from my comment from the other answer you just need to handle the window.onunload event and use the window.opener property to tell the calling page to be refreshed.

2.add.jsp

<html>
<head>
    <script type="text/javascript">

        //ADDED START
        window.onunload = refreshParent;
        function refreshParent() {
            window.opener.location.reload();
        }
        //ADDED END

        $(document).ready(function(){
            $("#savebtn").click(function(e) {
                $.ajax({
                    type: "POST",
                    url: "RecordHandler",
                    data: dataString,
                    success: function(data){ 
                         $('body').html(data);
                         $('#msg').html('New Record Added Successfully.');
                         window.timeout(CloseMe, 1000); <-- not sure on the syntax 
                         but have a timeout which triggers an event 
                        to close the form once a success has been handled. 
                        Probably should do something incase of an error.
                    }
                });

                return false; <-- this should stop the window from unloading. 
            });

         function CloseMe()
         {
             window.opener.location.reload();
             window.close();
         }
   </head>

You could use location.reload(true) to reload the current document. The forceGet parameter is by default false and that is why you pass it in as true to overwrite it. Basically it is used to fetch the document from the server, instead of loading it from the cache.

EDIT1: If you are trying to reload the source window of the popup, as escaparello mentioned in the comments, you should call window.opener.location.reload(). Also, you could bind an event listener for when the popup unloads, as follows:

popupWindow.onunload = function () {
    // This informs the user that the record has been added successfully
    alert('The record has been inserted into the database!');

    window.opener.location.reload();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!