2013-03-27 142 views
2

我想創建一個自定義的共享鏈接,所以點擊它將共享當前的網址。 我明白這推特/臉書分享動態網址

<a href="http://twitter.com/share?text=An%20Awesome%20Link&url=http://www.google.com"> 
Share This on Twitter</a> 

但是有什麼辦法讓它動,所以它會搶用戶是在頁面的URL和共享,而不是硬編碼的鏈接。

感謝

回答

0

使用jQuery:

$('a').on('click', function() { 
    document.location = 'http://twitter.com/share?text=' + document.title + '&url=' + window.location.href; 
}); 
2

在Javascript中你可以使用:

document.URL 

它給你當前的URL

https://developer.mozilla.org/en-US/docs/DOM/document.URL

document.title 

獲得此頁面的標題

https://developer.mozilla.org/en-US/docs/DOM/document.title

與jQuery或JavaScript可以設置href屬性

http://docs.jquery.com/Attributes/attr

https://developer.mozilla.org/en-US/docs/DOM/stylesheet/href

例子:

$('a').on('click', function() { 
    $(this).attr('href', 
     'http://twitter.com/share?text='+document.title+'&url=' + document.URL); 
}); 

另一種方式:

https://dev.twitter.com/docs/tweet-button

+0

嗨感謝您的建議,您瓊的作品,但它被引導到這個http://twitter.com/share?text=HNIB%20|% 20Tim%20Bret%20Day&url = http://tbd.aaronnicholls.co.uk/work/hnib/ – 2013-03-27 11:20:30

+0

我認爲文檔標題中的空格是加入%20% – 2013-03-27 11:22:07

+0

%20%是正常的,它的編碼 – 2013-03-27 11:23:18

0

使用此

<a data-count='horizontal' expr:href='data:post.canonicalUrl' href='http://twitter.com/share' rel='nofollow' target='_blank'>Share on twitter</a> 

expr:href='data:post.canonicalUrl'的伎倆。

儘管如此,twiiter給你一個選擇,而generating buttons