javascript - Need to POST and close window when user clicks submit button - Stack Overflow

Right now I have the following:<%= submit_tag 'Save' ,{:onClick => "window.close()

Right now I have the following:

<%= submit_tag 'Save' ,{:onClick => "window.close()"} %>

This works well for closing the window. Unfortunately, it closes the window before the form POSTs to the server.

The window was created using a link in the homepage:

function amenitiesPopup(){
  window.open("/amenities", "Swag", "status,width=600,height=800,resizable,scrollbars=yes","POS"); 
}

Without the :onClick => "window.close()" the form POSTs as expected. Just need to close it afterwards.

Thanks

Edit: My solution was to have the controller return a string literal:

if (params[:search])
    ...
    render :text => '<body onload="window.close()"></body>'
end

This will skip any layout/view items and just render an empty body that auto-closes

Right now I have the following:

<%= submit_tag 'Save' ,{:onClick => "window.close()"} %>

This works well for closing the window. Unfortunately, it closes the window before the form POSTs to the server.

The window was created using a link in the homepage:

function amenitiesPopup(){
  window.open("/amenities", "Swag", "status,width=600,height=800,resizable,scrollbars=yes","POS"); 
}

Without the :onClick => "window.close()" the form POSTs as expected. Just need to close it afterwards.

Thanks

Edit: My solution was to have the controller return a string literal:

if (params[:search])
    ...
    render :text => '<body onload="window.close()"></body>'
end

This will skip any layout/view items and just render an empty body that auto-closes

Share Improve this question edited Sep 27, 2013 at 18:28 Chris asked Sep 27, 2013 at 17:42 ChrisChris 9596 silver badges18 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Just return

<body onload="window.close()">

from the server, that way your POST is sure to go through before the window closes. So instead of closing from the submitting document, you close in the document received as reply by the server. Also, if you want to close the window afterwards, you might consider posting to an iframe instead depending on what you want to achieve.

function onSubmit(){
    $.ajax({
        //POST info and URL etc goes here
    }).done(function(){
        window.close();
    });
}

You need to specify all arguments required by the jquery ajax api (url, data, etc).

if you submit without ajax you'll go to the page you submitted to and will no longer have control of your window, unless you control the destination page, in which case you should add the window.close to the script of your destination page:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745090112a4610644.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信