2013-09-30 72 views
9

我有一个带有Facebook分享按钮的页面。我想分享的URL上有一个查询字符串,我用javascript构建。下面是我如何生成共享URL ..Facebook忽略共享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(); 
}); 

当Facebook抓住网址因为某些原因离开其关闭这是在queryString可变创造了一切。因此,共享网址最终只是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上的实际链接不完整。

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

回答

16

Facebook的URL出来,因为这:

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

第一&和参数cup(以及其他参数)被解释为Facebook网址的一部分。

使用encodeURIComponent(),这将特殊字符编码像&