简体   繁体   中英

Facebook like/share current URL

I have been trying to solve this for a few days now. Basically I have a like/share button on my site generated using the facebook developer tool. So it is very standard. But I can't get it to like/share the current URL.

<div class="fb-like col-xs-offset-1" data-layout="standard" data-action="like" data-show-faces="true" data-share="true"></div> 

What I have tried:

1) Using data-href =" <%= Request.Url.AbsoluteUri %> "

2) Setting data-href using javascript using window.location

3) Leaving data-href empty. Now this is said to work, but it does not for me. However I suspect this is because I am running on localhost, could that be why?

When I try these methods it gives me this link before I get to the actual share-window:

https://www.facebook.com/v2.2/plugins/like.php?action=like&app_id=78205603350&channel=http%3A%2F%2Fstatic.ak.facebook.com%2Fconnect%2Fxd_arbiter%2FrFG58m7xAig.js%3Fversion%3D41%23cb%3Df2283ae550ed898%26domain%3Dlocalhost%26origin%3Dhttp%253A%252F%252Flocalhost%253A59928%252Ff3ee4865310c9fa%26relation%3Dparent.parent&container_width=1237&href=http%3A%2F%2Flocalhost%3A59928%2FDefault&layout=standard&locale=en_US&sdk=joey&share=true&show_faces=true

And then when I press next and come to the actual sharing-window it tries to share "www.facebook.com"

1) Must be <%=

data-href="<%=Request.Url.AbsoluteUri%>"

2) You could do

<div id="sharebutton"
...

window.onload = function () {
    var currentUrl = window.location.href;
    document.getElementById("sharebutton").setAttribute("data-href", currentUrl);
}

-- or using jquery

jQuery(document).on('ready', function($){
    var url = window.location;  
    $('.fb-like col-xs-offset-1').attr('data-href', url);
});

3) Leaving data-href "" blank worked before v2.0 of social plugin

You can't share localhost. You need to test your share-button on production server where you would have real URL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM