Search code examples
javascriptjqueryfacebookfacebook-like

Facebook share button / history.pushstate / wrong url


I am using the facebook like/share button

https://developers.facebook.com/docs/plugins/like-button/

to share the current url/page.

Furthermore my urls are created by using the javascript history.push function to modify the url.

    history.pushState("", "","/123");

So in the beginning my url is "example.com" afer a few seconds the url is modyfied and the new url is like "example.com/123".

The problem is that example.com is shared instead of example.com/123. Is there a way to share the new url?

Thank you


Solution

  • Simply updating the data-href attribute on the button will do nothing – it has already been parsed, and “no one cares” any more about what attributes you might change afterwards.

    You will have to remove the button from the document, replace it with a new one – and then call FB.XFBML.parse to get the new button parsed.