Twitter分享按钮显示空白页面.为什么

Twitter share button shows blank page. Why?

本文关键字:为什么 空白 显示 分享 按钮 Twitter      更新时间:2023-09-26

我想在博客帖子上分享我的twitter按钮。它在20个职位中的18个工作得很好。只有2个岗位有问题。单击按钮时出现空白窗口,没有文本,url和via等…

<<p> url/strong>
    迪士尼《小美人鱼》宣布全国巡演
  1. MLB场馆-球队的状态如何影响他们主场的票价

我的代码是好的还是这里发生了什么?

.tweet{ margin:0px auto; width:200px; text-align:center;}
.tweet a{ display:inline-block; line-height:50px; color:#f00; text-decoration:none; background:#ccc; border-radius:5px; padding:0px 20px;}
<div class="tweet">
<a title="Twitter" href="//twitter.com/intent/tweet?share=" onclick="tweetShare=window.open(this.href+escape(window.location)+'&text='+escape(document.getElementsByTagName('title')[0].innerHTML)+'&url='+location.href+'&via=ExciteEventstix','tweetShare','toolbar=no,status=no,width=640,height=400,scrollbars=no'); return false;"
target="_blank">Twitter</a></div>

查看Chrome开发者控制台,你会发现twitter服务器返回了一个400错误。这是因为您没有正确地编码url(特别是title参数)。

注意Disney后面的%u2019字符。它是一个编码的unicode字符。实际上twitter希望你将其编码为UTF-8。

text=National%20Tour%20Announced%20for%20Disney%u2019s%20The%20Little%20Mermaid

解决方案是使用encodeURIComponent()代替escape()。下面的代码应该可以正常工作:

<div class="tweet">
<a title="Twitter" href="//twitter.com/intent/tweet?share=" onclick="tweetShare=window.open(this.href+encodeURIComponent(window.location)+'&text='+encodeURIComponent(document.getElementsByTagName('title')[0].innerHTML)+'&url='+location.href+'&via=ExciteEventstix','tweetShare','toolbar=no,status=no,width=640,height=400,scrollbars=no'); return false;"
target="_blank">Twitter</a></div>

无论如何escape()已被弃用,所以请尝试encodeURIComponent

    在推特按钮文档中没有share参数支持。url就够了。
  1. document.titledocument.getElementsByTagName('title')[0].innerHTML一样完美。
  2. 如果不使用,不要创建额外的全局变量tweetShare

所以可以简化为:

<div class="tweet">
<a title="Twitter" href="javascript:window.open('https://twitter.com/intent/tweet?url='+encodeURIComponent(location.href)+'&text='+encodeURIComponent(document.title)+'&via=ExciteEventstix','tweetShare','toolbar=no,status=no,width=640,height=400,scrollbars=no')">Twitter</a></div>