0

Possible Duplicate:
How do I stop a page from unloading (navigating away) in JS?

I've created an online whiteboard. I wish to confirm from user whether he wants to leave the page or not (since he needs to save whiteboards before leaving)

$(window).bind('beforeunload', function(){
    return 'Are you sure you want to leave?';
});

Now I need to call a function if the user confirms to leave the page. That function will post to the DB from jQuery (which I know how to do) & convert the user from active to inactive.
Only trouble I'm having is how do i get that function to be executed when the user CONFIRMS to leave.
Thanks!


FURTHER...

$(window).bind('beforeunload', function(){
  return 'Are you sure you want to leave?';
});

window.onunload=pageleave();
function pageleave() {
    var grp=$("#grpid").val();
    var login=$("#loginid").val();
$.ajax({
    type: 'POST',
    url:'collwb_pageleave.php',
    data: { GrpID:grp, LogID:login },
    cache:false,
});
}


Here what happens is, on page load, pageleave gets executes and make Activity=0 in the Database. This should happen on page unload... Why? Why would this happen?

Community
  • 1
  • 1
P.C.
  • 651
  • 13
  • 30
  • Seek and you [shall](http://stackoverflow.com/questions/5102602/confirm-to-leave-the-page-when-editing-a-form-with-jquery) [find](http://stackoverflow.com/questions/1299452/how-do-i-stop-a-page-from-unloading-navigating-away-in-js) – afuzzyllama Feb 10 '12 at 18:29
  • You'll probably run into problems with this, as any open connections to the server (such as AJAX calls) would be aborted when the user leaves or closes the browser. – Mike Christensen Feb 10 '12 at 18:30
  • 2
    Look ak here: http://stackoverflow.com/questions/6063522/jquery-beforeunload – v.tsurka Feb 10 '12 at 18:31
  • Try adding `async: false` to your `$.ajax` in `unload`. – gen_Eric Feb 10 '12 at 20:33
  • @v.tsurka: Hey, I answered that :-) – gen_Eric Feb 10 '12 at 20:34
  • In my experience, doing stuff when the page unloads, has been very inconsistent cross-browser. I would not rely on it. – Sparky Feb 10 '12 at 20:39
  • 1
    You have `pageleave()`, which executes pageleave immediately. You want `window.onunload = pageleave;` instead! :) – Kato Feb 10 '12 at 22:12
  • @Kato Yep I want pageleave() to only be called on unload. And this is not working especially not in Google Chrome. I researched the net. And I suppose this is not a good technique. It creates cross-browser problems... – P.C. Feb 11 '12 at 13:32
  • @Sparky672 you are right. unload executes as planned on Safari but not Google Chrome :( – P.C. Feb 11 '12 at 13:33
  • @Kato Thx for all ur help. I guess I'll just have to find a work around. Trigger the event by some other way... Thx a lot! :) – P.C. Feb 11 '12 at 13:34

1 Answers1

0

As v.tsurka said, this has been answered here.

Essentially, you put it on the window unload object (it's not directly linked to your beforeunload event but triggered if "ok" is clicked in the beforeunload dialog.)

$(window).unload(...your function...)

There are quite a few limitations on it (discussed in the referenced post and specifications you might want to google).

As mentioned in this post, there are lots of things you can't even get a notice for (losing power, internet connection, etc). Your best bet might be a fast, simple, GET request with some URL parms to send important notes.

Also, since your talking about more of an app than a web page, if data loss is a big concern, you might consider an air app that syncs to a site rather than a web page product.

Community
  • 1
  • 1
Kato
  • 40,352
  • 6
  • 119
  • 149
  • I tried this--> $(window).unload(function(){alert('Bye.'); – P.C. Feb 10 '12 at 18:46
  • It gave me no alert in chrome :( – P.C. Feb 10 '12 at 18:46
  • is what I'm doing correct. Is this triggered if "ok" is clicked? – P.C. Feb 10 '12 at 18:50
  • I doubt you're allowed to block the thread in unload, as this could prevent a user from ever leaving your page (infinite popups!) – Mike Christensen Feb 10 '12 at 19:05
  • 1
    As the referenced post states, alert() is not allowed in Chrome. In general, if the user says "ok", no action on your part, short of becoming a god and ruling the universe, will stop them from leaving the page (that's the idea). – Kato Feb 10 '12 at 19:06
  • So, in case it's unclear, you put the confirm in the beforeunload, using a string (as you did in your example) and the code to utilize if they leave the page in window.unload. But window.unload will not stop them or allow you to open any dialogs to stop them. – Kato Feb 10 '12 at 19:07
  • @Kato I don't really want to give a pop-up on page unload. But is it possible for me to call a jQuery function on clicking the OK in onbeforeunload? OR any way by which I can check that the user is no longer on the page. I just want to make him inactive. – P.C. Feb 10 '12 at 19:09
  • @Kato Please see the additions I made to the question. Please help me out :) Thank you! – P.C. Feb 10 '12 at 20:26
  • Hopefully my comment above will help you with the unload. One thing you still seem to be missing is that the unload event is not connected to the "OK" button in any way. The beforeunload is really just that; it gives the user a chance to change their mind *before* the unload. If they charge onward, then the unload occurs (`window.onunload` or `$(window).unload(function() {...})` is invoked) – Kato Feb 10 '12 at 22:14
  • @Kato tried it out but window.onunload or $(window).unload(function() {...}) does not occur no matter what I do. This could be browser specific or maybe not. I m trying on Google Chrome. – P.C. Feb 11 '12 at 13:36
  • Are you still trying the alert? That's never going to work. Try console.log with your debugger (F12) opened. – Kato Feb 13 '12 at 16:51