2013-09-30 12 views
9

Ho una pagina con un pulsante Condividi di Facebook. L'URL che voglio condividere ha una stringa di query su di esso che costruisco con javascript. Ecco come sto generando l'URL da condividere ..Facebook ignora parte della mia stringa di query nell'URL della condivisione

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

Quando facebook afferra l'URL per qualche motivo la sua lasciando fuori tutto ciò che è stato creato nella variabile queryString. Quindi l'URL condiviso finisce per essere solo http://example.com/?share=1. Qualche idea sul perché abbia lasciato la variabile queryString? L'URL corretto viene inserito nella console.log bene, più la sua nell'URL share.php di Facebook come una stringa di query (ad esempio https://www.facebook.com/sharer/sharer.php?u=http://example.com/?share=1&cup=blue&bowl=red&spoon=green) .. ma il collegamento effettivo su Facebook è incompleto.

Ecco un jsFiddle. http://jsfiddle.net/dmcgrew/gawrv/

risposta

16

L'URL facebook esce come questo:

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

Il primo & e il parametro cup (così come gli altri parametri) vengono interpretate come parte dell'URL facebook.

Usa encodeURIComponent(), che codificare i caratteri speciali come &: