Twitter分享按钮动态URL共享

Rya*_*per 3 html javascript twitter

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

这就是我到目前为止所说的

<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>
Run Code Online (Sandbox Code Playgroud)

由于我对JS很不熟悉,所以它有点被黑了.

wen*_*ntz 5

这会将url和title传递给一个带有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>
Run Code Online (Sandbox Code Playgroud)

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

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


VVL*_*VVL 0

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