English 中文(简体)
在Twitter上用查询字符串共享URL
原标题:Sharing a URL with a query string on Twitter

我正在尝试在电子邮件中添加一个Twitter共享链接。因为这是在一封电子邮件中,我不能依赖JavaScript,必须使用“构建你自己的”推文按钮。

例如,共享谷歌链接:

<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>

带有查询字符串的URL混淆了Twitter的URL缩短服务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_

我已尝试对整个URL进行编码。当我这样做时,推文中不会显示URL。

最佳回答

这可以通过使用来解决https://twitter.com/intent/tweet而不是http://www.twitter.com/share。使用<code>intentit/tweet</code>函数,您只需对整个URL进行URL编码,它就像一个魅力。

https://dev.twitter.com/web/intents

问题回答

这对你有用

http://twitter.com/share?text=text goes here&url=http://url goes here&hashtags=hashtag1,hashtag2,hashtag3

这是一个关于它的实例


http://twitter.com/share?text=Im在Twitter上分享;url=https://stackoverflow.com/users/2943186/youssef-subehi&;hashtags=stackoverflow,例如,youssefusf

使用推特网络意图,这是一个简单的链接:

https://twitter.com/intent/tweet?url=<?=urlencode($url)?>

更多变量,请访问https://dev.twitter.com/web/tweet-button/web-intent

它对我有效:

<div class="links">
    <ul>
    <li class="social-share facebook">Share on Facebook</li>
    <li class="social-share twitter">Share on Twitter</li>
    <li class="social-share linkedin">Share on LinkedIn</li>
    </ul>
</div>

在js文件中添加以下内容:


setShareLinks();

    function socialWindow(url) {
    var left = (screen.width -570) / 2;
    var top = (screen.height -570) / 2;
    var params = "menubar=no,toolbar=no,status=no,width=570,height=570,top=" + top + ",left=" + left;  window.open(url,"NewWindow",params);}

function setShareLinks() {
    var pageUrl = encodeURIComponent(document.URL);
    var tweet = encodeURIComponent($("meta[property= og:description ]").attr("content"));

$(".social-share.facebook").on("click", function() { url="https://www.facebook.com/sharer.php?u=" + pageUrl;
socialWindow(url);
});

$(".social-share.twitter").on("click", function() {
url = "https://twitter.com/intent/tweet?url=" + pageUrl + "&text=" + tweet;
socialWindow(url);
});

$(".social-share.linkedin").on("click", function() {
    url = "https://www.linkedin.com/shareArticle?mini=true&url=" + pageUrl;
socialWindow(url);
})
}

希望这会很好。

您可以使用以下功能:

 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>

你必须改变&;从您的url到查询字符串中的%26

看看这个:https://dev.twitter.com/discussions/8616

我参考了所有的方法。

对查询进行两次编码是快速解决方案。

const query = a=123&b=456;
const url = `https://example.com/test?${encodeURIComponent(encodeURIComponent(query),)}`;


const twitterSharingURL=`https://twitter.com/intent/tweet?&url=${url}`

你不一定需要使用intent,如果你对URL进行编码,它也可以与twitter共享一起使用。

正如@onteria_所提到的,您需要对整个参数进行编码。对于其他面临相同问题的用户,您可以使用以下bookmarklet生成正确编码的url。将其复制粘贴到浏览器的地址栏中,以创建twitter共享url。当你将其复制到地址栏时,请确保javascript:前缀存在,谷歌Chrome在复制时会将其删除。

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))})();

Source on JS Fiddle http://jsfiddle.net/2frkV/

如果你在html网站上手动添加它,只需替换:

&

具有

&amp;

标准html代码;

Twitter现在允许您通过数据属性发送URL。这对我来说很好:

<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>




相关问题
How can I make a query string tamper-proof?

I need to use query strings in a URL, but need to ensure that they haven t been tampered with. I found a solution which almost works, but the encoded strings get mangled by a service my app needs to ...

Passing Lists as a QueryString - should I use JSON

Lets say I ve got an online dating site, users can filter the user list based on various criteria, Height, Age, BodyType, Ethnic Origin.... I want to pass the criteria to the pager, via QueryString. ...

Is using URL query string variables still RESTful

I m presented with a problem of using query string variables in a RESTful Asp.net MVC application. Does that violate the RESTful pattern? The thing is that Asp.net MVC uses default route like: /...

URL requests adding POST data to querystring

I am using ExtJS to send an Ajax request to a PHP page on a server, wanting to send the parameters as POST variables rather than in the querystring. I have included a random token in the querystring ...

Maintain the querystring in all pages in mvc

I need to maintain the querystring in all pages in my asp.net mvc(C#) application. For ex.: I will call a page www.example.com?Preview=True. The querystring should be maintained whatever the page i ...

Nginx 301 redirect inc. set cookie

what I m looking for is the ability for nginx to detect that a url has the query string variable cid. for example www.adomain.com/froggy?cid=12&as=false (query string can be solo or mixed with ...

ASP.NET: URI handling

I m writing a method which, let s say, given 1 and hello should return http://something.com/?something=1&hello=en. I could hack this together pretty easily, but what abstraction functionality ...

Checking querystring values in PHP

http://localhost/?area=characters&name=Michal+Stroganof $result = mysql_query("SELECT * from players WHERE name = $_GET[name] "); while ($row = mysql_fetch_assoc($result)) { echo "Name: " ....

热门标签