views:

64

answers:

1

Hi,

given to certain circumstances, I'm forced to keep page settings (Javascript-values) in the session and it has to be done right before leaving the page (I can't use cookies, since "pageSettings" can become quite large and localStorage is not an option yet ;) ). So this is how I tried it. However it seems that when I call the page directly again, the call of "http://blabla.com/bla" happens asynchronous, even though the async-attribute is set (I don't receive the settings of the previous call, but of the one before):

$jQ(document).ready(function () {
    $jQ(window).unload(Main.__setSessionValues);
});

var Main = {
    pageSettings: {},

    __setSessionValues: function __setSessionValues() {
        $jQ.ajax({
            type: "POST",
            async: false,
            url: "http://blabla.com/bla",
            data: {
                pageSettings: Object.toJSON(Main.pageSettings)
            }
        });
    }
};

Does anyone know what the problem might be?

thanks in advance

+1  A: 

The code looks fine. You might try bind('beforeunload', ...) rather than unload, to grab things as early as possible. But of course, if something else also hooks beforeunload and the unload gets cancelled, your call will have been made even though you're still on the page.

Slightly off-topic, but if you can possibly find a different way to do this, I would. Firing off synchronous ajax calls when the user is trying to leave the page is not ideal.

T.J. Crowder
I also thought about the beforeunload-event, but I recall that I've read somewhere that it doesn't work in IE, so I skipped it... However I've tried it now and it works... Even in IE(7) ;)Thanks
Prjio
@Prjio: Glad that helped. `beforeunload` does work in IE (even IE6), what you've probably heard about is that the mechanism for showing the user a message from the `beforeunload` is a bit different in IE than in some other browsers. But since you're not showing a message... :-)
T.J. Crowder
Obviously I mixed that up... It appears that beforeunload used to be a IE-event (http://www.quirksmode.org/js/events_events.html -> Microsoft events). But it also works in FF (in the meantime, at least ;) )... Anyway, thanks for the tip with the message from the beforeunload event :)
Prjio