2014-03-19 44 views
-1

我正在嘗試爲我的網站創建自定義社交分享按鈕,但我遇到了當前頁面標題和網址未被正確拉伸以與社交網絡共享的問題。我的代碼抓取頁面標題和當前網址的代碼

例子是這樣的:

<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> 

任何幫助或在正確的方向點是極大的讚賞。

謝謝!

回答

0

在onclick事件中使用Javascript獲取document.titledocument.location.href,然後將其附加到共享URL並隨後將新URL設置爲。

所以,你的onclick屬性看起來像:

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