Search code examples
asp.netjqueryonbeforeunload

Jquery help needed for onbeforeunload event


I am using jquery onbeforeunload event in asp.net application. If i write event as given below then its working fine and display confirm dialog box.

var vGlobal = true;
var sMessage = "Leaving the page will lost in unsaved data!";

[ Working ]

> window.onbeforeunload = function() {
>   if (vGlobal == false) return
> sMessage; }

but its not working if i use bind method like as given below

[ Not working ]

$(window).bind("beforeunload", function(e) {
    if (vGlobal == false)
        return sMessage;
});

Anybody suggest me why its not working.Is there any difference between these two methods.

Code on aspx:

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>

CLICK ON THIS LINK TO SEE RUNNING EXAMPLE


Solution

  • See the updated version

    You need to bind all the events inside document ready event.