2013-03-27 136 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