简体   繁体   English

如何使用JavaScript重新填充Facebook共享对话框

[英]How to repopulate Facebook Sharing Dialog with javascript

What I'm trying to do is to make a form field that counts checked radio buttons with a specific value(integer 7). 我正在尝试做的是创建一个表单字段,该表单字段以特定值(整数7)对选中的单选按钮进行计数。 If the number of checked radio button is more than 3, then the system populates "Facebook" sharing dialog. 如果选中的单选按钮的数量大于3,则系统将填充“ Facebook”共享对话框。

It works well if nothing goes wrong, but when I close the popup dialog, and click more radio buttons, even if the count is higher than 3, the Facebook dialog doesn't popup. 如果没有问题,它会很好地工作,但是当我关闭弹出对话框并单击更多单选按钮时,即使该计数高于3,Facebook对话框也不会弹出。 I suspect that this is the problem due to initialization but I have no clue how to fix the issue. 我怀疑这是初始化引起的问题,但是我不知道如何解决该问题。 ( And because I suspect, I am not sure this guess is also directing me to the right solution ) (而且因为我怀疑,所以我不确定这种猜测是否也将我引向正确的解决方案)

The below is my javascript code to work this out. 下面是我的JavaScript代码来解决这个问题。 I really hope to share any thought! 我真的希望分享任何想法!

Best 最好

<script>
  $(".evaluation").click(function(){
    console.log('clicked');
    var count = 0;
    $(".evaluation").each(function(index,item){
      if (item.value == 7){
        if (item.checked == true){
          count ++;
        }
      }
    });

    console.log('count:' + count);

    if (count >= 3){

    window.fbAsyncInit = function() {
        FB.init({
          appId      : '####',
          xfbml      : true,
          version    : 'v2.5'
        });

        FB.ui({
      method: 'share_open_graph',
      action_type: 'og.likes',
      action_properties: JSON.stringify({
        object:'http://www.###.com',
      })
    }, function(response){
      // Debug response (optional)
      console.log(response);
    });
  };

  (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 = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
  }
  }

  );


</script>

You need to use window.fbAsyncInit and FB.init on Page load, one time. 您需要FB.init在页面加载时使用window.fbAsyncInitFB.init NOT on button click. 不要单击按钮。 Don't try to load and initialize the JavaScript SDK again, i assume the callback function will not get called because it is already initialized. 不要尝试再次加载和初始化JavaScript SDK,我假设不会调用回调函数,因为它已经初始化。

For example: http://www.devils-heaven.com/facebook-javascript-sdk-login/ 例如: http : //www.devils-heaven.com/facebook-javascript-sdk-login/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM