小编典典

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

jsp

我有一页包含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>
  1. 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> 

阅读 199

收藏
2020-06-08

共1个答案

小编典典

根据我对其他答案的评论,您只需要处理该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>
2020-06-08