0

I'm catching the page close/refresh event by following code:

window.onbeforeunload = function(){
  jQuery("#submit_form_button").click();
  jQuery("#saving_loader").show();
};

Here, the line jQuery("#submit_form_button").click();sends a request to controller#update method and saves the form before reload. The only problem is, it works sometimes and sometimes not. Specifically I'm seeing this problem in Chrome.

So I want to delay the refresh process. It should happen after click() event. I tried setTimeout() but it doesn't work.

Any suggestions?

Abhi
  • 4,123
  • 6
  • 45
  • 77
  • you tried `setTimeout`? How you tried? Post your trial! – Guruprasad J Rao Jun 08 '15 at 14:03
  • 1
    `setTimeout` runs code in the global scope so it won't have access to the variables in the context if you have variable names in the string, also, *Chrome* does not allow cookies to save when run from a local file on your computer unless you open *Chrome* with a special flag. so if you are saving your form in cookies it won't appear in *Chrome* – Sarfaraaz Jun 08 '15 at 14:05
  • @Sarfaraaz I'm not saving form in cookies. When the page refreshes, it sends the **current page url** again to controller and renders same page again. My idea is to send an update request before request with **current page url** hits controller – Abhi Jun 08 '15 at 14:11
  • 1
    I think `beforeunload` must return a string before closing the window.. take a look at http://stackoverflow.com/questions/7389554/crossbrowser-onbeforeunload – Daniel Almeida Jun 08 '15 at 14:13
  • _"So I want to delay the refresh process. It should happen after click() event. I tried setTimeout() but it doesn't work."_ Would `window` be closed when `duration` of `setTimeout` reached ? – guest271314 Jun 08 '15 at 14:16
  • or you can use jQuery http://stackoverflow.com/a/18822757/536532 – Daniel Almeida Jun 08 '15 at 14:18
  • @DanielAlmeida Thanks. I had already tried your first suggestion. The problem is **return** STRING will open a popup which I don't want. All the saving should happen silently. – Abhi Jun 08 '15 at 14:20
  • I visited a website some time ago they put a big message and with a grey overlay over the original page on the `document.onblur` event. this goes into affect with the user moves the mouse to click the close button. no luck if they ***alt+F4*** – Sarfaraaz Jun 09 '15 at 06:54

0 Answers0