html - Twitter/Facebook 分享动态 URL

标签 html facebook twitter hyperlink share

我正在尝试创建自定义共享链接,因此单击它将共享当前 URL。 我明白这一点

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

但是有没有办法让它变得动态,这样它就会抓取用户所在页面的 URL 并共享它,而不是硬编码的链接。

谢谢

最佳答案

在 Javascript 中你可以使用:

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

关于html - Twitter/Facebook 分享动态 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15656695/

相关文章:

javascript - HTML5 历史 API : cannot go backwards more than once

php图片上传安全防范黑客攻击

python - 在 Twython 中执行搜索时收到异常

php - 使用php将json形式的twitter响应解析到mysql数据库

python - 使用 tweepy 在 Twitter 上获取给定用户的所有 friend

html - 打印网页

facebook - 如何使用 Windows Azure ACS 从 Facebook 获取更多声明?

javascript - facebook javascript SDK : FB. Canvas.setAutoResize iFrame 不工作?

javascript - 异步调用 facebook api 时丢失范围

html - 粘性页脚问题