sivasakthi
sivasakthi

Reputation: 101

How to stop refresh the page form the beforeunload event?

i'm using the 'beforeunload' event to detect the refresh event from the webpage.how to stop refresh the page from beforeunload event and i should not show alert message

 window.addEventListener("beforeunload", this.onUnload);


onUnload = e => { 
    e.preventDefault();
  // how to stop refresh the page from here and i should not show alert message.

   //it is showing alert message. i no need to show the alert.
    e.returnValue = "sure do you want to reload?";   
    
 }

Upvotes: 0

Views: 2626

Answers (2)

Endunry
Endunry

Reputation: 974

When i understand your following comment corectly, then you simply need to return zero, or false. So in your function you write:

onUnload = e => { 
//e.preventDefault();
return false;
}

When i do this, i get an automated response from the Browser (for me its Chrome), if i really wanna close this website. And its in my language, so its i18n-compilant

It basically says "Are you sure you wanna refresh/close this website, data may get lost" and than 2 buttons with "Refresh/Close" and "Abort" This is what im seeing

Upvotes: 0

Charlie
Charlie

Reputation: 23798

It is not possible to prevent unloading a page without notifying the user.

Imagine you want to go to github.com at a time you are viewing stackoverflow.com - but it will simply prevent you from navigating away!

However, there was a time some browsers used to prevent unloading silently when you assign an empty string to the returnValue. But I believe that age of evil is gone now.

Upvotes: 1

Related Questions