簡體   English   中英

與 LinkedIn Share API 共享時出現 Javascript 錯誤

[英]Javascript error when sharing with LinkedIn Share API

我有以下代碼:

 $(".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

您需要對每個參數進行編碼,請參閱LinkedIn 文檔

 $(".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

例如,您需要正確編碼。

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 Developer Network 文檔說永遠不要直接使用 encodeURIComponent(),而是使用這個函數。 看一看: 關於 encodeURIComponont() 的 MDN Web 文檔

我也修正了你的網址, /share-offsite/ shareArticle? /share-offsite/是新標准, shareArticle? 是已棄用的舊標准。 來源: LinkedIn 文檔:在 LinkedIn 上共享,部分:“自定義 URL”

最后, titlesummarysource參數也都已棄用。 您只能在 HTML 源代碼的<head>塊中使用og:標簽來填充這些字段。例如,這看起來像...

  • <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 文檔:使您的網站可在 LinkedIn 上共享

想看看你做對了嗎? LinkedIn Post Inspector 上測試您自己的網站。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM