如何在javascript中关闭弹出窗口后刷新父页面?

Dev*_*esk 3 javascript jquery jsp popupwindow

我有一个页面list.jsp列出了表格中的所有记录,一个按钮位于顶部以添加新记录.

我想打开add.jsp一个弹出窗口.这有效但当我关闭弹出窗口时如何更新, list.jsp以便它显示新添加的记录

这是我的代码,我试过...

  1. 的List.jsp

    <html>
    <head>
    <script>
       function popupwindow(url, title, w, h) {
        var left = (screen.width/2)-(w/2);
        var top = (screen.height/2)-(h/2);
        popupWindow =  window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
         return popupWindow
       } 
    </script>
    </head>
    <body>
    <input type="button" value="Add new" id="add-btn" onclick="popupwindow('Addnew.jsp','Add new',600,400)"/>  
    <table>   
      // Here i am showing all records from database...
    </table>
    </body>
    
    Run Code Online (Sandbox Code Playgroud)
  2. add.jsp

         <html>
         <head>
         <script type="text/javascript">
         $(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.')
                        }
                    }); 
           });
    
          </head>
          <body>
          <form method="POST">
          <table>              
          <tr>
           <td>Folder Number</td>
           <td><input type="text" name="folderno"/></td>
         </tr>
         <tr>
            <td>Box Number <b style="color:red">*</b></td>
           <td><input type="text" name="boxno"/></td>
        </tr>
        <tr>
         <td colspan=2>
          <input type="submit" value="Save" name="save" id="savebtn"/>
        </td>
      </tr>
       </table> 
     </form> 
    
    Run Code Online (Sandbox Code Playgroud)

vla*_*zam 6

您可以使用location.reload(true)重新加载当前文档.forceGet默认情况下false,该参数是您传递它true以覆盖它的原因.基本上它用于从服务器获取文档,而不是从缓存中加载它.

编辑1:如果您正在尝试重新加载弹出窗口的源窗口,如评论中提到的escaparello,您应该调用window.opener.location.reload().此外,您可以在弹出窗口卸载时绑定事件侦听器,如下所示:

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();
}
Run Code Online (Sandbox Code Playgroud)

  • 它应该是`window.opener.location.reload(true);`如果你是从弹出窗口调用它. (4认同)

Sec*_*rel 5

从我对其他答案的评论中你只需要处理window.onunload事件并使用该window.opener属性来告诉刷新调用页面.

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>
Run Code Online (Sandbox Code Playgroud)