2013-09-30 2 views
9

Facebook 공유 버튼이있는 페이지가 있습니다. 공유하고 싶은 URL에 자바 스크립트로 빌드 한 쿼리 문자열이 있습니다. 여기에 내가 공유 할 수있는 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(); 
}); 

페이스 북은이 queryString 변수에 생성 된 모든를 떠나 어떤 이유로 URL을 잡고합니다. 따라서 공유 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/

답변

16

페이스 북의 URL이 같이 나오는 :

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

첫 번째 & 및 매개 변수 cup (뿐만 아니라 다른 매개 변수)가 페이스 북의 URL의 일환으로 해석됩니다.

& 같은 특수 문자를 인코딩합니다 사용 encodeURIComponent() :

shareURL = encodeURIComponent(siteURL+queryString);