all articles

facebook share window duplicates when multiple calls

2015-07-30 @sunderls

facebook share window duplicate open js

description, bug

window.open always create new window for facebook share, while twitter share won't cause duplication.

code

$button.on('click', function(){
    window.open('http://www.facebook.com/sharer/sharer.php?u=" + encodeURIComponent('http://colla.me'), 
        "fb", 
        "width=200,height=200, resizable=yes,scrollbars=no");
        })

every time $button is clicked, a new window prompts.

twitter share is just fine

twitter share url is http://twitter.com/share?url=.....

why

if we pass a name to window.open, window.open(url, name), no matter how many times you call, there would be only one window. But this doesn't work for facebook share.

Dig into the facebook share window, in the console, try logging window.name.

'_a_d22'

yes, facebook change the window.name every time accessed, so the name passed in would not work.

solution

keep a track of the window by yourself, never trust facebook

if (!window.fb || (window.fb && window.fb.closed)){
    window.fb = window.open('http://www.facebook.com/sharer/sharer.php?u=" + encodeURIComponent('http://colla.me'), 
        "fb", 
        "width=200,height=200, resizable=yes,scrollbars=no");
        })
} else {
    window.fb.focus();
}