How does stackoverflow customize the confirm()
box's buttons and add custom text to the buttons?
All the other questions I looked at pointed at a random jQuery
plugin to do this. Can this be done with Vanilla Javascript? If yes, then how?
How does stackoverflow customize the confirm()
box's buttons and add custom text to the buttons?
All the other questions I looked at pointed at a random jQuery
plugin to do this. Can this be done with Vanilla Javascript? If yes, then how?
There are only specific messages/buttons you can offer the user, without using a custom alert box/plugin. They are:
To display a message with just an OK button, by calling:
To display a message with OK/Cancel as the options, by calling:
As StackOverflow does in your example, to display a Leave.../Stay... message on leaving the page, where pressing Stay causes the browser to stay on your page (the exact button wording is browser-dependent, you cannot control it):
For any other situation, you'll need to use some kind of alert plugin (or write one yourself :-/), as you speculated in your question.
Put simply, those buttons are browser specific and chrome automatically displays helpful text on the button to make things clearer. It happens with
window.onbeforeunload
if you return a string, which is displayed as a message before navigation.