代码之家  ›  专栏  ›  技术社区  ›  Mario83

如何附加LinkedIn共享标题?

  •  -1
  • Mario83  · 技术社区  · 8 年前

    我使用jQuery获取主标题部分,但我想在标题末尾添加一些自定义文本。我很确信我把撇号弄错了,但我不知道在哪里。

    我想添加的文本是“自定义共享文本”。

    这就是我被困的地方:

        <a href="#" onclick="window.open('http://www.linkedin.com/shareArticle?mini=true&url=' + jQuery('.active-review').attr('href') + '&title=' + jQuery('.active-review .head-container').text() + 'CUSTOM SHARE TEXT', '', '_blank, width=500, height=500, resizable=yes, scrollbars=yes'); return false;">
        <i class="fa fa-fw fa-linkedin"></i> LinkedIn</a>
    

    谢谢你的帮助。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Mario Cianciolo    8 年前

    我认为您的问题与URL中的空格和其他无效字符有关。

    您应该始终使用 encodeURIComponent 从文本构建URL时。

    此外,正如您正确指出的,您忘记了一些撇号。

    尝试以下操作:

    <a href="#" onclick="window.open('http://www.linkedin.com/shareArticle?mini=true&url=' + encodeURIComponent(jQuery('.active-review').attr('href')) + '&title=' + encodeURIComponent(jQuery('.active-review .head-container').text()) + encodeURIComponent('CUSTOM SHARE TEXT'), '', '_blank', 'width=500, height=500, resizable=yes, scrollbars=yes'); return false;">
        <i class="fa fa-fw fa-linkedin"></i> LinkedIn
    </a>
    
        2
  •  -1
  •   Mario83    8 年前

    问题在于jQuery组件中的空格过多。添加后 trim() 在我的情况下,问题似乎得到了解决。附加到标题的文本始终位于此处,但由于数十个 %20 标题后。

    这是我的最终代码。

        <a href="#" onclick="window.open('http://www.linkedin.com/shareArticle?mini=true&url=' + encodeURIComponent(document.URL) + '&title=' + encodeURIComponent(jQuery('.active-review .head-container').text().trim()) + encodeURIComponent(' CUSTOM TEXT'),'', '_blank, width=500, height=500, resizable=yes, scrollbars=yes'); return false;"
            ><i class="fa fa-fw fa-linkedin"></i> LinkedIn</a>