Javascript Facebook忽略共享URL中我的部分查询字符串
我有一个带有Facebook共享按钮的页面。我想要共享的URL上有一个查询字符串,我使用javascript构建该字符串。下面是我如何生成要共享的URLJavascript Facebook忽略共享URL中我的部分查询字符串,javascript,jquery,facebook,facebook-sharer,Javascript,Jquery,Facebook,Facebook Sharer,我有一个带有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 t
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();
});
当facebook出于某种原因获取URL时,它会删除在queryString
变量中创建的所有内容。因此,共享URL最终只是http://example.com/?share=1
。你知道为什么要去掉queryString
变量吗?正确的URL会被放入控制台.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 facebook的URL如下所示:
https://www.facebook.com/sharer/sharer.php?u=http://example.com?share=1&cup=blue&bowl=red&spoon=green
第一个&
和参数cup
(以及其他参数)被解释为facebook url的一部分
使用encodeURIComponent()
,它将编码特殊字符,如&
:
shareURL = encodeURIComponent(siteURL+queryString);
除了JasonP的答案之外,
share.php
早就被弃用了
相反,您应该使用Facebook提要和共享对话框:
它们提供了对共享对话框的更多控制,以及通过更好的调试。截至2014年10月,Facebook已弃用sharer.php和Facebook提要与共享对话框 当前建议使用“共享”对话框进行链接共享:
我在这个特定场景中遇到了这个问题: 我将分享以下链接:
FB.ui({
method: 'share',
href: 'mysite.com?p=10&og=15',
}, function(response){});
Facebook scraper将从URL中删除og
参数,并将刮取以下URL:mysite.com?p=10
原因
mysite.com?p=10&og=15
。scraper将向给定URL发出请求,并得到如下响应:
//更多元标记。。。
og:url
meta标记中证明的任何值,并向该url发出请求,然后抓取该页面og:url
meta标记的值,以包含所有必需的参数
<head>
<meta property="og:url" content="mysite.com?p=10">
<meta property="og:image" content="mysite.com?photo=1145">
// more meta tags...
</head>