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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19100333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:20:46  来源:igfitidea点击:

Facebook ignoring part of my query string in share URL

javascriptjqueryfacebookfacebook-sharer

提问by Dustin

I have a page with a Facebook Share button. The URL I want to share has a query string on it that I build with javascript. Here is how I'm generating the URL to share..

我有一个带有 Facebook 共享按钮的页面。我想共享的 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();
});

When facebook grabs the URL for some reason its leaving off everything that was created in the queryStringvariable. So the shared URL ends up being just http://example.com/?share=1. Any ideas why its leaving off the queryStringvariable? The correct URL gets put into the console.logjust fine, plus its in the Facebook share.php URL as a query string (for example https://www.facebook.com/sharer/sharer.php?u=http://example.com/?share=1&cup=blue&bowl=red&spoon=green).. but the actual link on Facebook is incomplete.

当 facebook 由于某种原因获取 URL 时,它会忽略在queryString变量中创建的所有内容。所以共享 URL 最终只是http://example.com/?share=1. 任何想法为什么它离开queryString变量?正确的 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 上的实际链接不完整。

Here is a jsFiddle. http://jsfiddle.net/dmcgrew/gawrv/

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

回答by Jason P

The facebook URL comes out as this:

facebook URL 如下所示:

https://www.facebook.com/sharer/sharer.php?u=http://example.com?share=1&cup=blue&bowl=red&spoon=green

The first &and the parameter cup(as well as the other parameters) are interpreted as part of the facebook url.

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

Use encodeURIComponent(), which will encode special characters like &:

使用encodeURIComponent(),它将对特殊字符进行编码,例如&

shareURL = encodeURIComponent(siteURL+queryString);

回答by ceejayoz

In addition to Jason P's answer, sharer.phpis long since deprecated.

除了 Jason P 的回答,sharer.php早就被弃用了。

Instead, you should use the Facebook Feed and Share dialogs: https://developers.facebook.com/docs/reference/dialogs/feed/

相反,您应该使用 Facebook Feed 和 Share 对话框:https: //developers.facebook.com/docs/reference/dialogs/feed/

These offer more control over the share dialog, as well as better debugging via the Facebook Debugger.

这些提供了对共享对话框的更多控制,以及通过Facebook 调试器更好的调试。

回答by Glen

As of October 2014, Facebook have deprecated both the sharer.php and the Facebook Feed and Share dialogs.

截至 2014 年 10 月,Facebook 已弃用 sharer.php 和 Facebook Feed 和 Share 对话框。

The current recommendation for link sharing is to use the Share Dialog:

当前链接共享的建议是使用共享对话框:

https://developers.facebook.com/docs/sharing/reference/share-dialog#redirect

https://developers.facebook.com/docs/sharing/reference/share-dialog#redirect