2013-09-26 91 views
3

我有一个页面list.jsp有一个表中的所有记录列表和一个按钮在顶部添加新的记录。如何在javascript中关闭弹出窗口后刷新父页面?

我想打开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> 
    
  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> 
    
+0

看到这个答案http://stackoverflow.com/questions/10792408/open-popup-and-refresh-parent-page-on-close-popup –

+0

谢谢Squirerl其工作..... –

+0

好听:) –

回答

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> 
+0

但它不显示“新记录已成功添加”。味精...用户如何知道该记录添加成功? –

+0

通过''click function''你已经声明'savebtn',你可以'返回false'。 然后有另一个'function'在'success'里被触发,该''成功'关闭了窗体。那么它在结束之前等待成功 –

+0

已经更新了我的答案,试图展示我试图在上面勉强接受 –

5

您可以使用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(); 
} 
+0

我认为,主要问题是“如何发现弹出已关闭”,而不是“如何重新加载页面” –

+0

我已经使用location.reload(真的),但它不刷新页面... –

+1

也任何人想要使用location.reload()应该检查这个http://stackoverflow.com/questions/2405117/difference-between-window-location -href-window-location-href-and-window-location –

相关问题