How to change emphasis on confirm javascript buttons?

1k views Asked by At

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?

2

There are 2 answers

0
Leo Bauza On

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.

0
starikovs On

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.