views:

69

answers:

1

I have this jQuery code;

$(function () {
    $(window).unbind("beforeunload");
    $(window).bind("beforeunload", function () {
        return confirm("Really?");
    });
});

When i close my window I get the confirmation request and if I hit "Cancel" I get a second confirmation which says;

"Are you sure you want to navigate away from this page?"

"False"

"Press OK to continue, or Cancel to stay on the current page."

Why am I getting a second dialog and is there a way to remove it?

edit

have changed the code to be;

$(function () {
    $(window).bind("beforeunload", function () {
        return "slappy?";
    });
});

But the confirmation message does not appear. The event is firing because I can put an alert in there and see the alert.

edit 2

Have changed the code to this;

window.onbeforeunload = function () {
    var txtBlog = $('#tbxNote').val();
    if (txtBlog != "")
        return "You have not saved your blog entry.";
}

it works but there is other text above and below my message;

"Are you sure you want to navigate away from this page?"

"You have not saved your blog entry."

"Press OK to continue, or Cancel to stay on the current page."

+4  A: 

Just use:

return "Really?"; 

The beforeunload event requires you to return a string containing the message you want displayed in the standard "Are you sure" dialog.

Saxon Druce
And in case you didn't know and were wondering... there is no possible way to override the built-in "Are you sure..." and "Press OK to..." messages.
Cory Larson
The confirm is not firing;$(function () { $(window).bind("beforeunload", function () { return ("slappy?"); });}); Any thoughts?
griegs
Do you mean you've gone from two dialogs to one dialog, or to zero dialogs?
Saxon Druce
have gone to zero dialogs.
griegs
Excuse me for my plain remark, but this is awesome.
Valentin Vasiliev