Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
82
rated 0 times [  86] [ 4]  / answers: 1 / hits: 75159  / 14 Years ago, sat, july 10, 2010, 12:00:00

If the user refreshes the page in question it will add another record to the database, so I want to warn the user through an alert box if they really want to refresh the page and if they click ok then the page should be refreshed otherwise if they click cancel it won't be.



How to make this type of alert box appear when the browser's refresh button is clicked in a way that is cross browser compatible?


More From » javascript

 Answers
12

You can do it like this:



window.onbeforeunload = function() {
return Data will be lost if you leave the page, are you sure?;
};


This would show a prompt to the user allowing them to cancel. It's not refresh specific, but for your purposes (like editing a question on SO) that doesn't seem to matter, it's loss of info no matter where you're leaving to.


[#96276] Thursday, July 8, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pierre

Total Points: 716
Total Questions: 128
Total Answers: 102

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
;