Facebook在共享URL中忽略我的部分查询字符串

Dus*_*tin 11 javascript jquery facebook facebook-sharer

我有一个带有Facebook Share按钮的页面.我要共享的URL上有一个查询字符串,我用javascript构建.以下是我如何生成要共享的URL.

queryString = "cup=blue&bowl=red&spoon=green"; 
//the values of this are actually generated by user input, don't think its important for this example though. So in this example its just a basic string.

siteURL = "http://example.com/?share=1&";
//the url without the query string

sharingURL = siteURL+queryString; 
//Combing the domain/host with query string.. sharingURL should = http://example.com?share=1&cup=blue&bowl=red&spoon=green


function FBshare(){
  shareURL = siteURL+queryString;
  console.log(shareURL);
  window.open(
     'https://www.facebook.com/sharer/sharer.php?u='+shareURL, 
     'facebook-share-dialog', 
     'width=626,height=436'); 
  return false;
}


$(".facebook").bind("click", function(){
   FBshare();
});
Run Code Online (Sandbox Code Playgroud)

当facebook因某种原因抓取URL时,它会删除在queryString变量中创建的所有内容.所以共享的URL最终只是http://example.com/?share=1.任何想法为什么它离开queryString变量?正确的URL被放入正常的URL console.log,加上它在Facebook share.php URL中作为查询字符串(例如https://www.facebook.com/sharer/sharer.php?u=http://example.com/?share=1&cup=blue&bowl=red&spoon=green)..但Facebook上的实际链接是不完整的.

这是一个jsFiddle.http://jsfiddle.net/dmcgrew/gawrv/

Jas*_*n P 18

facebook的网址如下:

https://www.facebook.com/sharer/sharer.php?u=http://example.com?share=1&cup=blue&bowl=red&spoon=green
Run Code Online (Sandbox Code Playgroud)

第一个&和参数cup(以及其他参数)被解释为facebook url的一部分.

使用encodeURIComponent(),它将编码特殊字符,如&:

shareURL = encodeURIComponent(siteURL+queryString);
Run Code Online (Sandbox Code Playgroud)