I'm building a website with Facebook Connect and therefore using the Facebook Javascript SDK. Problem: when using Firefox, the page doesn't reload properly once logged in or logged out.
FB.Event.subscribe(
'{% if current_user %}auth.logout{% else %}auth.login{% endif %}',
function(response){
window.location.reload();
});
Obviously, it looks like a known problem (just type "window location reload not working on firefox" and you'll get a lot of results)
More precisely, Firefox doesn't seem to send the right cookie when reloading the page...
- When I click to login, once Facebook logged me in and sets a cookie, Firefox doesn't send any cookie
- When I click to logout, once Facebook has logged me out and remove the cookie, Firefox sends the cookie that was previously there.
I conclude it uses some "cache functions".
I tried to make a workaround as described here and implemented this:
redirect_url = encodeURIComponent(window.location.href);
url = window.location.href + "account/login?redirect_url=" + redirect_url;
window.location.replace(url);
But the problem remains (the cache I guess...) Can you help me out with this?
Thanks.
Try wrapping the window.location.reload() call in a setTimeout() with a zero delay. Apparently, Firefox fires the event before setting the cookie. The wrapping should place the reload call in the event queue and allow the cookies to be set correctly.
Incase there was anymore confusion over this, for Firefox, the above referenced example of setTimeout()
works perfectly. Simply include your reload call, or if it's easier just replace:
document.location.reload();
with:
setTimeout('document.location.reload()',0);
Using the most recent version of Firefox released, I have tested every example above, and this is the only one that has actually worked consistently. Basically, you just need to "pause" the JavaScript just for a moment to let the rest of the script catch up. I did not test this on Chrome or IE, but it does work flawlessly on Firefox.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With