The onbeforeunload event is a very unusual one, in my experience it's not a "real" event. It's more like a callback (or is that a call-in), used by the browser to get a custom "Do you really want to leave?" string. Just use the windows.onbeforeunload = .... method, it works.
Karl Rudd On Wed, Mar 5, 2008 at 4:10 AM, Jack Bates <[EMAIL PROTECTED]> wrote: > > Is there any support for the onbeforeunload event in jQuery? > > I contribute to an open source records management application which > employs jQuery. Some of our usability feedback has been that users > loose > because they don't realize they must submit a form before navigating > to > another page. Sounds silly, but I guess it's easy to click one of the > links on our edit page while in the middle of completing the long > form. > > I was inspired by Gmail, which warns that your message hasn't been > sent > when you navigate away from the message composition page. I want to > present a similar warning to our users, but have encountered problems: > > Apparently others have tried to achieve the same result with jQuery > and > also encountered problems: > > http://groups.google.com/group/jquery-en/browse_thread/thread/3cfcb3a37660292c > > I guess it's not possible, when the user navigates away, to display a > JavaScript prompt and either cancel the action, save the form or allow > the action to proceed, based on the user's feedback, since this would > enable JavaScript to keep the user on a page indefinitely. > > However I also gather some browsers support an onbeforeunload event; > that returning a string from an onbeforeunload event handler causes a > confirmation dialog to be displayed with that message and options to > proceed or cancel; and that this is what Gmail uses: > http://www.boutell.com/newfaq/creating/disableclose.html > > Unfortunately I can't seem to get jQuery to handle onbeforeunload. I > tried: > > $(window).bind('beforeunload', function (event) { return 'Are you > sure?' }) > > - and - > > $(window).bind('beforeunload', function (event) { event.returnValue = > 'Are you sure?' }) > > - but neither worked. The following does work for me: > > window.onbeforeunload = function () { return 'Are you sure?' } > > I gather others have had similar experience: > > > http://groups.google.com/group/jquery-en/browse_thread/thread/24002d2e3f22b2cf > > > http://groups.google.com/group/jquery-en/browse_thread/thread/ece8bed4087b6082 > > So my question is: is there support in jQuery for onbeforeunload which > I'm missing or misusing, or is there a reason that jQuery doesn't > support onbeforeunload? > > Thanks and best wishes, Jack >