1

Is there any way to catch a page refresh and pause it for a certain time period using javascript, jquery or whatever?

This is necessary to prevent accidentally pressing the refresh key amid an important work which is one-time ,and needs to be processed and saved before the page gets refreshed.

In one of my previous questions posted in this forum, I had to face this issue, which is still unsolved, at a certain point during the discussion of the question. I think posting it as an individual question is quite worth it. The said question is here .Edit part of the question contains the issue.

Scripting language is php. Or should I use any other language to achieve the goal?

Community
  • 1
  • 1
Istiaque Ahmed
  • 6,072
  • 24
  • 75
  • 141

3 Answers3

6

It appears this can be accomplished with window.onbeforeunload() like in this example.

<script type="text/javascript">
   window.onbeforeunload = function() {
       return "You sure you want to leave? You will lose all your work!";
   }
</script>
Community
  • 1
  • 1
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • suppose the user, decides not to leave the page. Will all the javascript variable value calculated previously on the page retain? – Istiaque Ahmed Jan 14 '12 at 12:38
  • From what I understand it is "before unload" so nothing gets unloaded until the user confirms they are sure they wish to leave the page. – Jeremy Harris Jan 14 '12 at 12:42
  • there might be something running in the technical back-end without the user knowing it. Those tasks need to be done b4 the refresh. If the user chooses to refresh the page, how to catch and pause the refresh to release it at a later time. – Istiaque Ahmed Jan 14 '12 at 12:44
  • That's impossible. You could try firing off an AJAX request in `onunload` but you cannot delay it - it would clearly be a bad thing if any website could do this. – ThiefMaster Jan 14 '12 at 12:47
  • @ThiefMaster, cillosis - can u give an idea as to the prob in the edit part of the question referenced to in the question of this post. – Istiaque Ahmed Jan 23 '12 at 12:21
0

I don't think there is a way to stop or pause the page refresh. Yes, you can give a warning, but to be safe you just fill user's cookie with current state of form's input fields with javascript at every form field change. User pressed radiobutton? Cookies updated. User updated input field? Cookies updated.

Then, on page load, just have php or javascript read cookies and fill the form.

Ranty
  • 3,333
  • 3
  • 22
  • 24
  • no ajax? if ajax, then my ques in the previous post still remains – Istiaque Ahmed Jan 18 '12 at 13:10
  • Ajax or cookies, your choice. I don't any major drawbacks in any of these aproaches unless you have extremely high-load website where people leave the page with unfinished form thousands times per day :) – Ranty Jan 18 '12 at 13:49
-1

i think you should use:

<body onbeforeunload="return 'are you sure you want to move away from this page?';">
Karim Seoudy
  • 117
  • 1
  • 10