Antoine Pelletier
Antoine Pelletier

Reputation: 3316

onBeforeUnload event to create this?

enter image description here

I was searching for an exemple of what i want, and stackOverflow gave me one. I want to reproduce the above and i'm pretty sure that is an unBeforeUnload event. I already used the 2 code below but no succes... i wonder why.

<script language="javascript" type="text/javascript">

$(window).bind('beforeunload', function () {

    if (confirm("Vous alliez quitter sans sauvegarder. Voulez-vous sauvegarder maintenant ?") == true) {
        document.getElementById('<%= btnEnregistrer.ClientID %>').click();
    }
});

//dosen't work

window.onbeforeunload = confirmExit;
function confirmExit() {
    if (confirm("Vous alliez quitter sans sauvegarder. Voulez-vous sauvegarder maintenant ?") == true) {
        document.getElementById('<%= btnEnregistrer.ClientID %>').click();
    }
}

//Dosen't work......

Upvotes: 1

Views: 314

Answers (1)

Filipe Merker
Filipe Merker

Reputation: 2446

You can achieve this by passing a method directly to the onbeforeunload state of the window object

window.onbeforeunload = function(e) {
    e = e || window.event;
    e.preventDefault = true;
    e.cancelBubble = true;
    e.returnValue = 'Your beautiful goodbye message';
}

or you could use the addEventListener handler:

window.addEventListener('beforeunload', function(e) {
    e = e || window.event;
    e.preventDefault = true;
    e.cancelBubble = true;
    e.returnValue = 'Your beautiful goodbye message';
});

Upvotes: 1

Related Questions