Search code examples
c#javascriptasp.netfacebookfacebook-sharer

different url for facebook share button


I would like to give a different url in Facebook share button rather than current page url.

I've tried to change url in asp.net behind side but I wasn't success.

I would be gratefulif anyone can help about this.

Some codes that I used, you can see bellow.

this is front side script

<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript">
</script>
<script type="text/javascript">
function fbs_click() {
u = location.href;
t = document.title;
window.open('http://www.facebook.com/share.php?u=' + encodeURIComponent(u) +
'&t=' + encodeURIComponent(t), 'share', 'toolbar=0,status=0,width=626,height=436');
return false;
}
</script>

this is share button

<a name="fb_share" runat="server" id="aFbShare" type="button" class="sharelink">Paylaş</a>

this is what I did on behind side

string wishListUrl = SEOHelper.GetWishlistUrl(NopContext.Current.User.CustomerGuid);
aFbShare.Attributes.Add("onclick", "fbs_click()");
aFbShare.Attributes.Add("target", "_blank");
aFbShare.HRef = "http://www.facebook.com/share.php?u=" + wishListUrl;

Solution

  • The issue you are having is this. The user clicks your button:

    <script type="text/javascript">
    function fbs_click() {
       u = location.href;
       t = document.title;  
       window.open('http://www.facebook.com/share.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'share', 'toolbar=0,status=0,width=626,height=436');
       return false;
    }
    </script>
    

    In this function, u is the current URL, t is the current title. You open up a new window to facebook, then return false. This means your link is never actually followed because the false tells the browser the click should not be handled any further. What you should be doing is modify the code-behind:

    string desiredTitle = "My Wishlist";
    string wishListUrl = SEOHelper.GetWishlistUrl(NopContext.Current.User.CustomerGuid);
    aFbShare.Attributes.Add("onclick", "fbs_click('" + wishListUrl + "', '" + desiredTitle + "')");
    

    And modify the client script to:

    <script type="text/javascript">
    function fbs_click(url, title) {
       window.open('http://www.facebook.com/share.php?u=' + encodeURIComponent(url) + '&t=' + encodeURIComponent(title), 'share', 'toolbar=0,status=0,width=626,height=436');
       return false;
    }
    </script>
    

    If you need to maintain your current title, just remove the title variable from the onclick call, and the function signature, and pull the title from the document as you were doing before.