1

I have implemented Facebook Share dialog with javascript and I am having consistency issues.

I have a button to open the dialog and when I do so from the page it automatically appears in an iframe on the page which is great! Exactly what I want.

Now here's the issue, I have another page that redirects to this exact same page and it is set to call the exact same function for the facebook share dialog if a get variable is set. It calls it but now it opens in a new window instead of in an iframe and I have no idea why. I will literally close the window and click on the button and it will open it perfectly in an iframe.

$(document).ready(function() {
        window.fbAsyncInit = function() {
            FB.init({
                appId            : '######',
                autoLogAppEvents : true,
                xfbml            : true,
                version          : 'v3.2'
            });

            @if(isset($_GET['share']))
                facebook_share();
            @endif
        };

        (function(d, s, id){
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) {return;}
            js = d.createElement(s); js.id = id;
            js.src = "https://connect.facebook.net/en_US/all.js";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));

        $('.fb-share').click(function() {
            facebook_share();
        });
    });

    function facebook_share(){
        FB.ui({
            method: 'share',
            mobile_iframe: true,
            href: '######',
        }, function(response){
            if (response && !response.error_message) {
                success.style.display = "block";
            } else {
                error.style.display = "block";
            }
        });
    }
CirqueM
  • 77
  • 2
  • 9

1 Answers1

0

Figured this out myself, simply setting a timeout before making the call to the function caused it to act the same way. Everything must not have been fully loaded upon the original call.

setTimeout(facebook_share, 3000);
CirqueM
  • 77
  • 2
  • 9
  • timeouts for those kind of things are just workarounds, they are not real solutions. you should never use FB.ui automatically without user interaction anyway, browsers may block the popup. dialogs should only open on user interaction. – andyrandy Feb 01 '19 at 07:59
  • @luschn It's automatic because.. user is on a page displaying a whole bunch of different events but I can't set the OG dynamically for each if I want them to have the full capabilities of the share dialog. So when they click share it takes them to the event that has the preset OG information. – CirqueM Feb 07 '19 at 23:27
  • ok, that is a weird solution...why not just share the other url? you really do not need to redirect...sounds like you are creating a problem that does not even exist, or maybe i misunderstood your problem... – andyrandy Feb 07 '19 at 23:58
  • @luschn Facebook new API rules unfortunately. If I want to use a share dialog that gives users the ability to post to feed, friends, pages and private messages then it has to be on a specific page and can't be done dynamically. Well... I can do it dynamically but then the user may only post to their own personal feed. – CirqueM Feb 08 '19 at 00:19