开发者

javascript : closing the window on a button click

I need to close a window on a button click using javascript .

However , before closing the window , I want to submit the form (I am using struts2. It is a java web app).

So how do I submit the data for saving and close the window at the same time . It is a pop up kind of window . The below is the jquery code , I use to submit the form.

   function fnSubmit() {                
         $('#adminpopup').attr('action','submitPopupAction').submit();开发者_如何学编程  
}   

Edit : I have one more constraint here . When I click on the submit button , the form gets submitted . In case there are errors while returning from the server side code , I will have to display the error message in the pop up . If it is successful , close the pop up .

I guess this cannot be achieved by simply calling javascript functions one after the other. Is there any way to achieve this ?


Submit the form to the server.

If it is the same domain, return

<script>
window.close()
</script>

from the server


You could use the JavaScript self.close() function (or window.close() for other windows), but it will show a security message if the window was not opened with window.open(). You should be able to call this after the form is submitted.

Using window.close


Just send the form and (on success) send a page back that merely issues window.close. This will only work if the window has been opened using window.open, so as a fallback option you should provide a human readable message in the window that says that they can safely close the popup window.


Just add this after the submit line:

window.setTimeout(function() {
   window.close();
}, 1000);

This will give one second to the form to be submitted (enough time to ensure the request has been sent) then close the window.


Try following:

Create a button and add there the onClick event. Close this window. Additionally connect the event *un*load to the body tag. At this event submit the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜