单击链接而不是自动加载启动Facebook Livechat?

时间:2018-03-10 13:41:38

标签: php facebook facebook-javascript-sdk facebook-php-sdk

FB几个月前推出了Livechat API。添加以下编码后,右下角会弹出一个小图标。

enter image description here

<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId            : 'your-app-id',
      autoLogAppEvents : true,
      xfbml            : true,
      version          : 'v2.11'
    });
  };
  (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/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
</script>
<div class="fb-customerchat" page_id="PAGE_ID"></div>

这意味着,当访问该网站的访问者时,它会自动加载。

我的问题是,是否可以点击链接打开此实时聊天而不是自动加载?

<a href="#">Click here to start FB livechat!</a>   // something like this

1 个答案:

答案 0 :(得分:1)

在按钮单击事件中包装FB.init。这样,在您点击其他内容之前,它不会初始化聊天元素。

普通的javascript方法:

<script>
  var narf = function() {
    FB.init({
      appId            : 'your-app-id',
      autoLogAppEvents : true,
      xfbml            : true,
      version          : 'v2.11'
    });
  };
  (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/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
</script>
<div class="fb-customerchat" page_id="PAGE_ID"></div>
<button onclick="narf();">Start Chat</button>

jQuery方法:

<script>
  $(function() {
      $("#openchat").click(function(){
          FB.init({
              appId            : 'your-app-id',
              autoLogAppEvents : true,
              xfbml            : true,
              version          : 'v2.11'
          });
      });
  });
  (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/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
</script>
<div class="fb-customerchat" page_id="PAGE_ID"></div>
<button id="openchat">Start Chat</button>

标准javascript中的延迟XFBML parse方法(如CBroe所述):

<script>
  var narf = function() {
      FB.XFBML.parse(document.getElementById('chat-wrapper'));
  };
  window.fbAsyncInit = function() {
    FB.init({
      appId            : 'your-app-id',
      autoLogAppEvents : true,
      xfbml            : false,
      version          : 'v2.11'
    });
  };
  (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/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
</script>
<div id="chat-wrapper">
    <div class="fb-customerchat" page_id="PAGE_ID"></div>
</div>
<button onclick="narf();">Start Chat</button>