我正在尝试在电子邮件中添加Twitter共享链接。因为这是在电子邮件中我不能依赖JavaScript,并且必须使用“Build Your Own”Tweet按钮。
例如,分享指向Google的链接:
<a href="http://www.twitter.com/share?url=http://www.google.com/>Tweet</a>
这很好用。我遇到的问题是URL有查询字符串。
<a href="http://www.twitter.com/share?url=http://mysite.org/foo.htm?bar=123&baz=456">Tweet</a>
带有查询字符串的网址混淆了Twitter的网址缩短服务,t.co.我已经尝试过以各种方式对此进行URL编码,并且无法正常工作。我得到的最接近的是这样做。
<a href="http://www.twitter.com/share?url=http://mysite.org/foo.htm%3Fbar%3D123%26baz%3D456">Tweet</a>
这里我只编码了查询字符串。当我这样做时,t.co成功地缩短了URL,但是在遵循缩短的链接时,它会将您带到编码的URL。我在地址栏中看到http://mysite.org/foo.htm%3Fbar%3D123%26baz%3D456
,并在浏览器中收到以下错误
未找到
在此服务器上找不到请求的URL /foo.htm?bar=123&baz=456。
我不知道如何解决这个问题。
编辑:Re:onteria_
我试过编码整个网址。当我这样做时,Tweet中没有显示任何URL。
这对你有用
http://twitter.com/share?text=text goes here&url=http://url goes here&hashtags=hashtag1,hashtag2,hashtag3
这是关于它的实例
使用Twitter Intent资源qazxswpoi
这可以通过使用https://twitter.com/intent/tweet
而不是http://www.twitter.com/share
来解决。使用intent/tweet
函数,您只需对整个URL进行URL编码,它就像魅力一样。
使用tweet web intent,这是一个简单的链接:
https://twitter.com/intent/tweet?url=<?=urlencode($url)?>
您必须从您的网址更改查询字符串中的&%26
你不一定需要使用意图,你编码你的URL也可以使用twitter共享。
您可以使用以下功能:
function shareOnFB(){
var url = "https://www.facebook.com/sharer/sharer.php?u=https://yoururl.com&t=your message";
window.open(url, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');
return false;
}
function shareOntwitter(){
var url = 'https://twitter.com/intent/tweet?url=URL_HERE&via=getboldify&text=yourtext';
TwitterWindow = window.open(url, 'TwitterWindow',width=600,height=300);
return false;
}
function shareOnGoogle(){
var url = "https://plus.google.com/share?url=https://yoururl.com";
window.open(url, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=350,width=480');
return false;
}
<a onClick="shareOnFB()"> Facebook </a>
<a onClick="shareOntwitter()"> Twitter </a>
<a onClick="shareOnGoogle()"> Google </a>
不会比这更简单:
<a href="https://twitter.com/intent/tweet?text=optional%20promo%20text%20http://example.com/foo.htm?bar=123&baz=456" target="_blank">Tweet</a>
正如@onteria_所提到的,您需要对整个参数进行编码。对于遇到相同问题的任何其他人,您可以使用以下书签来生成正确编码的URL。将其粘贴到浏览器的地址栏中以创建Twitter共享网址。当您将javascript:
复制到地址栏时,请确保javascript:(function(){var url=prompt("Enter the url to share");if(url)prompt("Share the following url - ","http://www.twitter.com/share?url="+encodeURIComponent(url))})();
前缀存在,Google Chrome会在复制时将其删除。
http://jsfiddle.net/2frkV/
来自JS Fiddle <a href="javascript:;" class="twitter-share-button" data-lang="en" data-text="check out link b" data-url="http://www.lyricvideos.org/tracks?videoURL=SX05JZ4FisE">Tweet</a>
Twitter现在允许您通过数据属性发送URL。这对我很有用:
https://dev.twitter.com/web/tweet-button/web-intent