javascript Twitter 分享按钮动态网址分享

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19227702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:48:44  来源:igfitidea点击:

Twitter Share button dynamic URL share

javascripthtmltwitter

提问by RyanSoper

I'm trying to make a custom Twitter button that will grab the page URL of the current page when clicked, which would obviously change dependent on which page it appears on, not unlike the default share button. However, I am unsure how to pass the page URL into the button.

我正在尝试制作一个自定义 Twitter 按钮,该按钮将在单击时获取当前页面的页面 URL,这显然会根据它出现在哪个页面上而改变,与默认共享按钮不同。但是,我不确定如何将页面 URL 传递到按钮中。

This is what I have thus far jsfiddle

这是我迄今为止所拥有的 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>

It's a bit hacked together as I'm pretty unfamiliar with JS.

由于我对 JS 非常不熟悉,所以它有点被黑客攻击了。

回答by wentz

This passes the url and title to a simple pop-up box with the twitter share page:

这会将 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>

If you don't want it to open in a popup box, you can use this instead of window.open:

如果您不希望它在弹出框中打开,您可以使用它代替window.open

window.location.href="https://twitter.com/share?url="+ encodeURIComponent(window.location.href)+"&text="+document.title;

Update:escape()function is deprecated, Replaced with encodeURIComponent()

更新:escape()功能已弃用,替换为encodeURIComponent()

回答by Drew Dahlman

I would suggest window.location - That will give you the current page url.

我建议 window.location - 这会给你当前页面的 url。

Problem is you're using JS inline in HTML - you can't do that like you're wanting to.

问题是你在 HTML 中使用内联 JS - 你不能像你想要的那样做。

I updated your fiddle with some vanilla js to show you a better way to do this.

我用一些 vanilla js 更新了你的小提琴,以向你展示一个更好的方法来做到这一点。

http://jsfiddle.net/4jF5N/1/

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

回答by VVL

Take a look on document.location:
location.hostname
location.pathname
http://www.w3schools.com/jsref/obj_location.asp

看看document.location:
location.hostname
location.pathname
http://www.w3schools.com/jsref/obj_location.asp