0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Webサイトにはてなブックマークボタンを配置する方法

Posted at

「Webサイトに共有ボタンを配置する」はシリーズになっています。

はじめに

Webサイトにはてなブックマークボタンを配置する方法を紹介します。

はてなブックマークボタンのHTML

はてなブックマークボタンのHTMLを配置するのみです。

<a href="https://b.hatena.ne.jp/entry/"
  class="hatena-bookmark-button"
  data-hatena-bookmark-layout="basic-label-counter"
  data-hatena-bookmark-lang="ja"
  title="このエントリーをはてなブックマークに追加">
  <img src="https://b.st-hatena.com/images/v4/public/entry-button/button-only@2x.png"
    alt="このエントリーをはてなブックマークに追加"
    width="20"
    height="20"
    style="border: none;" />
</a>
<script type="text/javascript"
  src="https://b.st-hatena.com/js/bookmark_button.js"
  charset="utf-8"
  async="async">
</script>

このHTMLをWebサイトに表示すると、以下のボタンが表示されます。
ボタンを押下すると、はてなブックマークの共有画面が開きます。

はてブボタン 共有画面
スクリーンショット 2022-04-06 14.10.31.png スクリーンショット 2022-04-06 14.09.10.png

class="hatena-bookmark-button" を付けることで bookmark_button.js がはてなブックマークボタンと認識し、ボタンが表示されます。

記事のURLは自動的に取得され、明示的に指定する必要がありません。
そのためHTMLを動的に構築する必要がなく、実装が楽です。

JSの読み込み

bookmark_button.js を非同期で読み込むのみです。

おわりに

これでWebサイトにはてなブックマークボタンを配置できました!

参考リンク

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?