0

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

3
  • 1
    that is pretty questionable for an user experience Commented Jun 14, 2010 at 7:31
  • 1
    OT: On IE, your code will result in two separate function objects for __setSessionValues. It's a bug in JScript. It's not a big deal for the quoted code (both functions are very small), but it's useful to know in case you're using that pattern for lots of stuff all over. The problem is when you do a named function expression (a function name() { ... } that you also assign to something at the same time). JScript processes it as a function declaration (creating a function), then processes it again later as a function expression, creating an entirely separate function object. Sadly. Commented Jun 14, 2010 at 7:36
  • @jAndy: I know that this solution is far from optimal. But as I said... Given the circumstances, I couldn't come up with a better idea. The call for putting the value into the session is insignificant. I've already tested that. You don't notice the block resulting from the synchronous call. @T.J. Crowder: Interesting. I didn't know that. I use named functions for debugging purposes (to avoid all those anonymous functions in the call stack). But I'll keep that in mind, for future use. Thanks for the tip :) Commented Jun 14, 2010 at 7:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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: 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... :-)
Obviously I mixed that up... It appears that beforeunload used to be a IE-event (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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.