2013-09-30 4 views
9

私はFacebookの共有ボタンがあるページがあります。共有したいURLには、JavaScriptでビルドしたクエリ文字列があります。 FacebookはそのがqueryString変数で作成されたすべてのものをオフのまま何らかの理由でURLをつかむときにここで共有URLのクエリ文字列の一部を無視しているFacebook

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

..私は共有するためのURLを生成しています方法です。したがって、共有されるURLはただちに http://example.com/?share=1になります。変数が queryStringから離れる理由は何ですか?正確なURLは console.logに入れられます。さらに、クエリー文字列(例: https://www.facebook.com/sharer/sharer.php?u=http://example.com/?share=1&cup=blue&bowl=red&spoon=green)としてFacebook share.php URLに追加されますが、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のURLの一部として解釈されます。 &などの特殊文字エンコードします

使用encodeURIComponent()、:ジェイソンPの答えに加えて