首页 > 解决方案 > 领英回调函数

问题描述

我想在单击链接的共享按钮后调用一个函数。

 <div class="linkedinShare ci-aling" linkedin data-url='{{url}}' data-title='{{title}}' data-summary="{{text}}" data-shares='linkedinshares'>{{linkedinshares}}</div>

这是我试图调用的脚本,但它只在页面加载时被调用,并且在单击链接中的共享按钮时不会被调用。我希望在单击共享按钮时调用我的功能。

$.getScript('http://platform.linkedin.com/in.js', function () {
    debugger
    IN.Event.on(IN, 'systemReady', handleLinkedInEvent);

    function handleLinkedInEvent(event) {
        debugger
        if (event) {
            EventService.UpdateEventAudit($scope.event_id, "LinkedIn", 
            GetUrlReferrer());
        }
    }
});

UpdateEventAudit 是我试图调用的函数。有人知道吗?

标签: javascriptjquerylinkedin-apilinkedin-jsapi

解决方案


如果我正确理解这一点,您希望能够在用户通过linkedin 共享时跟踪事件...

我确实尝试了你的代码,经过一些研发,找到了另一种调用 api 的方法......

创建了一个迷你笔,您可以在此处查看https://codepen.io/craigiswayne/pen/Bqqbjz

可以在此处找到有关此主题的文档:https ://developer.linkedin.com/docs/share-on-linkedin

IN.Event.on(IN, 'systemReady', function() {
    var shareLink = document.getElementById('shareLink');
    shareLink.onclick = function(){
      event.preventDefault();
      var params = {
        "comment": "Check out developer.linkedin.com! " + this.getAttribute('href'),
        "visibility": {
          "code": "anyone"
        }
      };

      IN.API.Raw("/people/~/shares?format=json")
      .method("POST")
      .body(JSON.stringify(params))
      .result(StackOverflowDemo.updateShareCount)
      .error(function(errorMessage){
        StackOverflowDemo.logOutput('error occurred');
        console.log(errorMessage);
      });
    };

    document.body.appendChild(shareLink);

  });

  var StackOverflowDemo = {
    updateShareCount: function(result){
      var existingCount = parseInt( document.getElementById('count').value );
      existingCount = isNaN(existingCount) ? 0 : existingCount;
      document.getElementById('count').value =  existingCount + 1;
      StackOverflowDemo.logOutput( 'updated count' );
      StackOverflowDemo.logOutput( 'Total Shares = ' +  document.getElementById('count').value );
      StackOverflowDemo.logOutput( 'View Share here ' + result.updateUrl );
    },
    logOutput: function(text){
      document.getElementById('output').value +=  text + '\n';
    }
  }


推荐阅读