-1

I want to make a confirmation before user leaving the page. If he says ok then it would redirect to new page or cancel to leave.

but issue is that when user refresh page using browser refresh btn in google chrome that time below error occur

enter image description here

I tired this code but doesn't work in react.js please help

  window.hideWarning = false;
    window.addEventListener('beforeunload', (event) => {
      if (!window.hideWarning) {
        event.preventDefault();
        event.returnValue = '';
      }
    });

Also try this code

window.onbeforeunload = function () { return ''; }.bind(this);
Mayur Vaghasiya
  • 1,383
  • 2
  • 12
  • 24
  • Does this answer your question? [React | How to detect Page Refresh (F5)](https://stackoverflow.com/questions/50026028/react-how-to-detect-page-refresh-f5) – ToujouAya Mar 10 '21 at 07:29
  • @ToujouAya Its works ! but when user refresh the page using browser refresh button that time this error occur – Mayur Vaghasiya Mar 10 '21 at 07:33

3 Answers3

1

Try this code it's work for me

 window.onbeforeunload = function () {
  if ("Some condition") {
     return '';
   }
 }.bind(this);
0

you can also use that

winodow.location.reload(true) winodow.location.reload(false)

0

Please use this:

console.log(window.performance.navigation)
Ihor Konovalenko
  • 1,298
  • 2
  • 16
  • 21
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 15 '22 at 18:04
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 15 '22 at 20:04