1

I have the following JavaScript:

var leave_page_confirm = true;

function save_data_check() {
    var msg;
    if (leave_page_confirm === true) {
        $('.input-right').each(function() {
            if (this.value) {
                msg = 'You have unsaved data on this page.';
                return false;
            }
        });
    }
    return msg;
}

window.onbeforeunload = save_data_check;

This confirm box displays when you have unsaved data in a form on my pages.

However, when it appears the emphasis (default button) is "Leave This Page" I would like the emphasis to be on "Stay on This Page" despite quite a bit of Googling, I can't seem to find any code that changes the emphasis of the buttons on a confirm alert.

Is there any way to accomplish this?

David Chavez
  • 617
  • 3
  • 17
Allenph
  • 1,875
  • 27
  • 46
  • possible duplicate of [JavaScript: How to select "Cancel" by default in confirm box?](http://stackoverflow.com/questions/1699582/javascript-how-to-select-cancel-by-default-in-confirm-box) – ctwheels Jun 10 '15 at 14:43
  • possible duplicate of [How to personalize alert buttons - window.confirm() and window.alert()](http://stackoverflow.com/questions/16929370/how-to-personalize-alert-buttons-window-confirm-and-window-alert) – Mogsdad Jun 10 '15 at 15:21

2 Answers2

0

You might want to refer to this question:

How to personalize alert buttons - window.confirm() and window.alert()

Basically, you can't. But you can make your own modals for this kind of thing and have full control over what it looks like and which buttons are emphasized.

Community
  • 1
  • 1
Leo Bauza
  • 275
  • 1
  • 8
0

You just can't do this.

Here's the docs https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

BTW, you can try to find out another way to say users about unsaved data. It might be a UI solution, some outlines, messages etc.

starikovs
  • 3,240
  • 4
  • 28
  • 33