LoginSignup
14
18

More than 5 years have passed since last update.

ブックマーク(お気に入り登録)ボタンの設置

Last updated at Posted at 2014-11-06

ブックマーク(お気に入り登録)ボタンの実装でちょっと躓いたのでメモ。
各ブラウザの挙動が違うので、jsを使ってブラウザごとの設定をします。

js

$(function() {
    $('#bookmarkme,#bookmarkme2').click(function() {
        if (window.sidebar && window.sidebar.addPanel) { // Mozilla Firefox Bookmark
            window.sidebar.addPanel(document.title,window.location.href,'');
        } else if(window.external && ('AddFavorite' in window.external)) { // IE Favorite
            window.external.AddFavorite(location.href,document.title); 
        } else if(window.opera && window.print) { // Opera Hotlist
            this.title=document.title;
            return true;
        } else { // webkit - safari/chrome
            alert('ブラウザ付属のブックマーク機能をご利用ください。' + (navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? '【 Command 】' : '【 Ctrl 】') + ' + 【 D 】ボタンを押すとブックマークできます。');
        }
    });
});


safariとchoromeはブックマークをさせることができないので、アラートで「ブラウザ付属のブックマーク機能をご利用ください。【 Command 】 + 【 D 】ボタンを押すとブックマークできます。」と表示させます。

(navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? '【 Command 】' : '【 Ctrl 】')
この記述でWindoes時とmac時のショートカットキーを切り替えて表示させています。

html

<a id="bookmarkme" href="" rel="sidebar" title="bookmark this page"><img src="/img/btn.png" alt=""></a>

スマホではsafariとchromeが大半でしょうから、ブクマボタンを付けない方がベターですね。
PCでもブラウザ間の挙動が違いすぎるので、基本的にはブクマ機能自体を付けない方が良さそうですw

引用:javascript - Add to favorites button - Stack Overflow

14
18
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
14
18