Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize the message "Changes you made may not be saved." for window.onbeforeunload?

I am testing in Google Chrome.

I did some search and found that someone is using:

window.onbeforeunload = function() {     if (hook) {       return "Did you save your stuff?"     }   } 

But when I use it, I still got the "Changes you made may not be saved." message. How can I change it to something I want?

like image 433
AGamePlayer Avatar asked Nov 13 '16 03:11

AGamePlayer


People also ask

Is it possible to display a custom message in the Beforeunload popup?

A quick note (since this is an old answer) - these days all major browsers don't support custom message in the beforeunload popup. There is no new way to do this.

What is the difference between Onbeforeunload and Onunload?

onbeforeunload Below are my findings on the iPad; Using window. onunload , I am able to get an alert when user navigates to a different page from myPage. html (either by clicking on some link or doing a Google search while on myPage.

What does Window Onbeforeunload do?

The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.


1 Answers

You can't, the ability to do this was removed in Chrome 51. It is widely considered a security issue, and most vendors have removed support.

Custom messages in onbeforeunload dialogs (removed):

A window’s onbeforeunload property may be set to a function that returns a string. If the function returns a string, then before unloading the page, a dialog is shown to have the user confirm that they indeed want to navigate away. The string provided by the function will no longer be shown in the dialog. Rather, a generic string not under the control of the webpage will be shown.

Comments

This shipped in Safari 9.1, and has been shipping in Firefox since Firefox 4. Safari considers this a security fix and assigned it CVE-2009-2197 (see https://support.apple.com/en-us/HT206171 ). Approved with the intent https://groups.google.com/a/chromium.org/d/msg/blink-dev/YIH8CoYVGSg/Di7TsljXDQAJ .

Specification

Established standard

Status in Chromium

Removed (launch bug) in:

  • Chrome for desktop release 51
  • Chrome for Android release 51
  • Android WebView release 51
  • Opera release 38
  • Opera for Android release 38
like image 198
Alexander O'Mara Avatar answered Sep 18 '22 09:09

Alexander O'Mara