Code to grab page title and current url

344 Views Asked by At

I'm trying to create custom social share buttons for my website but I'n running into issues that the current page title and url are not being pulled properly to be shared with the social networks.

example of my code looks like:

<a class="share-btn twitter-btn twitter-sharer-button" id="ref_tw" href="https://twitter.com/intent/tweet?title=<?php echo $title; ?>&amp;url=<?php echo urlencode($share_url);?>&amp;via=Udamonia"  onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=no,scrollbars=no,height=400,width=600');return false;"></a>

Any help or a point in the right direction is greatly appreciated.

Thanks!

1

There are 1 best solutions below

0
On

Use Javascript in the onclick event to grab the document.title and document.location.href which you then append to the share URL and subsequently set the new URL to.

So your onclick attribute would look like:

onlick="javascript:window.open('http://twitter.com/intent?title='+encodeURIComponent(document.title)+';url='+encodeURIComponent(document.location.href);"