gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:18:55 25 4
gpt4 key购买 nike

我有一个带有 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();
});

当 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 上的实际链接不完整。

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

最佳答案

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

关于javascript - Facebook 忽略了共享 URL 中我的部分查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19100333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com