I try to make a message on the server before the page is uploaded, and I followed this up and it works fine. My problem is $ .post on window.unload starts after it is unloaded. I tried this with a link to the sign and checked my logs, I get the following:
Started GET "/signout" for 127.0.0.1 at 2012-11-22 00:15:08 +0800 Processing by SessionsController#destroy as HTML Redirected to http://localhost:3000/ Completed 302 Found in 1ms Started GET "/" for 127.0.0.1 at 2012-11-22 00:15:08 +0800 Processing by HomeController#index as HTML Rendered home/index.html.erb within layouts/application (0.4ms) Rendered layouts/_messages.html.erb (0.1ms) Completed 200 OK in 13ms (Views: 12.9ms) Started POST "/unloading" for 127.0.0.1 at 2012-11-22 00:15:08 +0800 Processing by HomeController#unloading as */* Parameters: {"p1"=>"1"} WARNING: Can't verify CSRF token authenticity Completed 500 Internal Server Error in 0ms NoMethodError (undefined method `id' for nil:NilClass): app/controllers/home_controller.rb:43:in `unloading'
The first part is the output, and then the user is redirected to root , then the post starts ('/ unloading').
Is there a way to make '/ unloading' execute first and then execute everything that has been unloaded?
I have it as my jquery post
$(window).unload -> $.ajax { async: false, beforeSend: (xhr) -> xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content')) , url: '/unloading' , type: 'Post' , data: { p1: '1' } }
Update
So, I passed an ajax request to beforeunload, and it worked, but I had to do return null to remove the dialog box, because if I do not, ajax still launches the popup dialog box (even without answering yes / no, I want to leave this page "). Result:
window.onbeforeunload -> $.ajax { async: false, beforeSend: (xhr) -> xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content')) , url: '/unloading' , type: 'Post' , data: { p1: '1' } } return null
Also, I only tried this with Chrome at the moment, and it works as expected. However, try other browsers.