LoginSignup
61

More than 5 years have passed since last update.

twitterのツイートボタンを作るためのリンクshareとintent/tweet

Last updated at Posted at 2017-01-29

オリジナルツイートボタンを作りたい

突如「指定のテキストとハッシュタグ入りのツイートのウィンドウが出るボタン」を作ることになった。

指定の形でaタグを作れば、いつものtweetボタンが作れるらしい。

参考文献

さっくり作るならここ
http://blog.yuhiisk.com/archive/2015/10/12/twitter-share.html

パラメーターの触り方はここ
http://kiteretsu-world.info/blog/2015/07/05/how-to-make-tweet-button/

コード

参考文献を読む前に、拝借してきたコードを使ったら「シェアじゃなくてツイートで」とクライアントから言われた。

shareの方

<a href="https://twitter.com/share?text=テキストテキスト%0a改行もできるし%20スペースも&url=https://www.url.url/&hashtags=タグ,二つ目" onClick="window.open(encodeURI(decodeURI(this.href)), 'tweetwindow', 'width=650, height=470, personalbar=0, toolbar=0, scrollbars=1, sizable=1'); return false;" rel="nofollow" class="twitter-link">
twitterでshare
</a>

intent/tweetの方

<a href="https://twitter.com/intent/tweet?text=テキストテキスト%0a改行もできるし%20スペースも&url=https://www.url.url/&hashtags=タグ,二つ目" onClick="window.open(encodeURI(decodeURI(this.href)), 'tweetwindow', 'width=650, height=470, personalbar=0, toolbar=0, scrollbars=1, sizable=1'); return false;" rel="nofollow" class="twitter-link">
tweet
</a>

URLが違うだけで、とりあえずツイートボタンの挙動としては大差ない。
パラメーターの使い方も一緒。

使って便利↓↓
改行:%0a
空白:%20
hashtagsは,(カンマ)で区切れば複数指定可能

そしてもって、shareとintent/tweetですが……intentの方はいろいろできるみたい。
Web Intents

機会があれば。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
61