与LinkedIn Share API共享时出现Javascript错误

问题描述 投票:0回答:2

我有以下代码:

 $(".btn_li").click(function() {
    window.open("http://www.linkedin.com/shareArticle?mini=true&url='+document.URL+'&title=Webpage Title;summary=Webpage Summary", "LinkedIn", "width=660,height=400,scrollbars=no;resizable=no");
try { 
 _gaq.push(['_trackEvent', 'Social', 'LinkedIn', "Page URL"]); 
 } catch(err){}        
return false;
}); 

并通过以下方式致电:

<div class="btn_li"></div>

单击此按钮时,我收到的LinkedIn错误是,“发生了意外问题,使我们无法完成您的请求。”这告诉我参数未正确传递。

有什么建议吗?

注意:这是我最初提出的问题:Add Google Analytics Click (event) tracking code to Javascript window.open

javascript
2个回答
2
投票

您需要对每个参数进行编码,请参见LinkedIn docs

 $(".btn_li").click(function() {

    var articleUrl = encodeURIComponent('http://medium.com');
     var articleTitle = encodeURIComponent('Meduim');
     var articleSummary = encodeURIComponent('Blog posts');
     var articleSource = encodeURIComponent('Medium');
     var goto = 'http://www.linkedin.com/shareArticle?mini=true'+
         '&url='+articleUrl+
         '&title='+articleTitle+
         '&summary='+articleSummary+
         '&source='+articleSource;
     window.open(goto, "LinkedIn", "width=660,height=400,scrollbars=no;resizable=no");        
return false;
}); 

示例:jsFiddle


0
投票

例如,您需要对此进行正确编码。

function fixedEncodeURIComponent(str) {
  return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
    return '%' + c.charCodeAt(0).toString(16);
  });
}

$(".btn_li").click(function() {
    var url = fixedEncodeURIComponent('http://yourURL.com');
    window.open("https://www.linkedin.com/sharing/share-offsite/?url=" + url);
});

Microsoft开发人员网络文档说,永远不要直接使用encodeURIComponent(),而要使用此功能。看一下:MDN Web Documentation on encodeURIComponont()

我还修复了您的网址,/share-offsite/是新标准,shareArticle?是已弃用的旧标准。来源:LinkedIn Documentation: Share On LinkedIn, Section: "Customized URL"

最后,titlesummarysource参数也都已弃用。您只能在HTML源代码的og:块中使用<head>标记来填充这些字段。例如,这看起来像...

  • <meta property='og:title' content='Title of the article"/>
  • <meta property='og:image' content='//media.example.com/ 1234567.jpg"/>
  • <meta property='og:description' content='Description that will show in the preview"/>
  • <meta property='og:url' content='//www.example.com/URL of the article" />

来源:LinkedIn Documentation: Making Your Website Shareable on LinkedIn

想看看你做对了吗?在LinkedIn Post Inspector中测试您自己的网站。

© www.soinside.com 2019 - 2024. All rights reserved.