开发者

Facebook Javascript SDK window location reload not working on Firefox

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 lo开发者_JAVA技巧gout, 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.


We are using the work-around with

window.location.href = url;

instead of

window.location.replace(url);

Works fine in Firefox, too.


For anyone using Rails and Koala, you can just check for the cookies and redirect to the same action if they are not present:

def fb_authenticate
  @fb_cookies ||= Koala::Facebook::OAuth.new.get_user_info_from_cookie(cookies)
  if @fb_cookies
    #login user or create new user
  else
    redirect_to :controller => 'account', :action => 'fb_authenticate'
  end
end


Try redirect to a different URL each time:

top.location.href = 'https://apps.facebook.com/YOUR_APP_NAME/?t=' + <?php echo time(); ?>;

, I use PHP but you can replace with JS ( Math.round(+new Date()/1000) I guess ).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜