推特分享按钮动态网址分享
Twitter Share button dynamic URL share
我正在尝试制作一个自定义Twitter按钮,该按钮在单击时将抓取当前页面的页面URL,这显然会根据它出现在哪个页面上而变化,这与默认共享按钮不同。但是,我不确定如何将页面URL传递到按钮中。
这就是我到目前为止所拥有的 jsfiddle
<style type="text/css" media="screen">
</style>
<div id="social-share-container">
<div id="custom-tweet-button">
<a id="tweetShare" href="https://twitter.com/share?url="+encodeURIComponent(document.URL); target="_blank">Tweet
</a>
</div>
</div>
它有点被黑客入侵,因为我对 JS 很不熟悉。
这会将 url 和标题传递到带有 Twitter 共享页面的简单弹出框中:
<script language="javascript">
function tweetCurrentPage()
{ window.open("https://twitter.com/share?url="+ encodeURIComponent(window.location.href)+"&text="+document.title, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');return false; }
</script>
<a class="tweet" href="javascript:tweetCurrentPage()" target="_blank" alt="Tweet this page">Tweet this page</a>
如果您不希望它在弹出框中打开,您可以使用以下内容代替window.open
:
window.location.href="https://twitter.com/share?url="+ encodeURIComponent(window.location.href)+"&text="+document.title;
更新: escape()
函数已弃用,替换为encodeURIComponent()
我建议window.location - 这将为您提供当前页面网址。
问题是你在 HTML 中使用内联 JS - 你不能像你想要的那样这样做。
我用一些香草js更新了你的小提琴,向你展示了一种更好的方法。
http://jsfiddle.net/4jF5N/1/
var link = document.getElementById('tweetShare');
var url = window.location;
link.addEventListener('click',function(event){
event.preventDefault();
window.open("https://twitter.com/share?url="+encodeURIComponent(url));
alert(url)
},false);
看看 document.location:
位置.主机名
位置.路径名
http://www.w3schools.com/jsref/obj_location.asp
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 动态分配GA增强型电子商务跟踪器
- onkeyup无法动态创建多个文本区域
- 使用javascript将动态表从一个html页面打印到另一个html页
- 如何使Javascript动态html表及其上的事件
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 如何使用javascript从主svg对象动态创建svg视图框
- 试图在引导模式内动态生成图表,得到offsetWidth错误
- 通过在Dojo mobile ViewController.openExternalView中动态更改打开同一外部视图的
- 如何在Google柱状图中动态添加行/列
- 困在逻辑中试图定位动态的东西
- 在动态创建的元素上获取对特定选择器的引用
- 动态脸书分享按钮
- 通过jQuery的动态Facebook分享按钮
- 推特分享按钮动态网址分享
- 与Google +分享动态内容图片javascript
- 如何在静态站点上动态呈现社交分享按钮的元标签
- 在facebook上分享动态(javascript生成)页面