Encoding jump anchors for Facebook shares – via sharer.php

Encoding jump anchors for Facebook shares proved to be an annoying, but simple task! Try it out for yourself.

https://www.facebook.com/sharer/sharer.php?u=http://example.com#jumplink 

Post to “Only Me” publicity if you wish (quite handy for developing). You will see when you take a look at facebook, he actually keeps the #jumplink anchor when you get redirected back to your facebook.com. It’s pretty silly.

The solution is a simple one – Facebook (or possibly how they are retrieving their $_GET parameters) is removing (or ignoring) our jump link. To fix this, we need to encode the special character to it’s ASCII equivalent – %23.

$url = 'http://example.com#jumplink';
$sharer_url = 'https://www.facebook.com/sharer/sharer.php?u=' . str_replace( '#', '%23', $url );

// http://example.com%23jumplink

The Result

Opening in a new window

Encoding jump anchors to Facebook shares when using JavaScript to create a new popup window is a very similar approach. You just use JavaScript’s replace method.

var url = 'http://example.com#jumplink';
var sharer_url = 'https://www.facebook.com/sharer/sharer.php?u=' + url.replace( '#', '%23');

// http://example.com%23jumplink

2 thoughts on “Encoding jump anchors for Facebook shares – via sharer.php”

Leave a Reply

Your email address will not be published. Required fields are marked *