Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript onbeforeunload not showing custom message

I have custom message to onbeforeunload event and was working well but I noticed today that it's not showing my message anymore. Instead it shows "changes you made may not be saved"

window.onbeforeunload = function () {      return 'Custom message' } 

can anyone please let me know how to fix it?

like image 336
Tushar ani Avatar asked Jun 13 '16 05:06

Tushar ani


People also ask

Is it possible to display a custom message in the Onbeforeunload 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 triggers Onbeforeunload?

The onbeforeunload event occurs when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page. The default message that appears in the confirmation box, is different in different browsers.

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

To avoid scamming, chromium and hence chrome have decided to remove the ability to set a custom message in the onbeforeunload dialog.

See this bug report from the 18th of February, 2016.

onbeforeunload dialogs are used for two things on the Modern Web:
1. Preventing users from inadvertently losing data.
2. Scamming users.

In an attempt to restrict their use for the latter while not stopping the former, we are going to not display the string provided by the webpage. Instead, we are going to use a generic string.

Firefox already does this[...]

like image 116
Kaiido Avatar answered Sep 21 '22 05:09

Kaiido