52

Drupalを使用してWebサイトを構築しています。各ページのヘッダーに、カスタムの[お気に入りに追加]ボタンとして機能する単一の画像(私がカスタムデザインしたもの)が必要です。画像をクリックすると、WebサイトのURLがユーザーのブラウザのお気に入り(ブックマーク)に追加されます。これは、IE7 +、FF、Opera、Chromeのすべてのブラウザで機能するはずです。私はこれに関する多くの情報をオンラインで見つけることができませんでした。私はjavascriptがその仕事をするべきだと思いますが、私はJavascriptの経験があまりないので、あなたの助けが必要です!

4

5 に答える 5

89

jQueryバージョン

$(function() {
  $('#bookmarkme').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('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a id="bookmarkme" href="#" rel="sidebar" title="bookmark this page">Bookmark This Page</a>

于 2012-04-05T17:36:27.943 に答える
22

このコードは、iambriansreedの回答の修正バージョンです。

<script type="text/javascript">
    $(function() {
        $("#bookmarkme").click(function() {
            // Mozilla Firefox Bookmark
            if ('sidebar' in window && 'addPanel' in window.sidebar) { 
                window.sidebar.addPanel(location.href,document.title,"");
            } else if( /*@cc_on!@*/false) { // IE Favorite
                window.external.AddFavorite(location.href,document.title); 
            } else { // webkit - safari/chrome
                alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');
            }
        });
    });
</script>
于 2013-03-01T14:26:19.800 に答える
2

rel="sidebar"でいくつかの問題に直面しました。リンクタグに追加すると、ブックマークはFFで機能しますが、他のブラウザでは機能しなくなります。だから私はコードによって動的なrel="sidebar"を追加することによってそれを修正します:

jQuery('.bookmarkMeLink').click(function() {
    if (window.sidebar && window.sidebar.addPanel) { 
        // Mozilla Firefox Bookmark
        window.sidebar.addPanel(document.title,window.location.href,'');
    }
    else if(window.sidebar && jQuery.browser.mozilla){
        //for other version of FF add rel="sidebar" to link like this:
        //<a id="bookmarkme" href="#" rel="sidebar" title="bookmark this page">Bookmark This Page</a>
        jQuery(this).attr('rel', 'sidebar');
    }
    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('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');

    }
});
于 2013-10-03T15:07:03.330 に答える
1
  if (window.sidebar) { // Mozilla Firefox Bookmark
     window.sidebar.addPanel(document.title,location.href,"");

ブックマークを追加しますが、サイドバーに追加します。

于 2012-04-16T23:54:16.677 に答える
0

@ Gert Grenander@ Alaa.KhRossShanonの功績

注文しようとしています:

それはすべて機能します-Firefoxのブックマーク機能を除いてすべて。何らかの理由で、「window.sidebar.addPanel」はデバッガーの関数ではありませんが、正常に機能しています。

問題は、呼び出し元のタグから値を<a ..>取得することです。タイトルはブックマーク名、hrefはブックマークアドレスです。これが私のコードです:

javascript:

$("#bookmarkme").click(function () {
  var url = 'http://' + location.host; // i'm in a sub-page and bookmarking the home page
  var name = "Snir's Homepage";

  if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1){ //chrome
    alert("In order to bookmark go to the homepage and press " 
        + (navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 
            'Command/Cmd' : 'CTRL') + "+D.")
  } 
  else if (window.sidebar) { // Mozilla Firefox Bookmark
    //important for firefox to add bookmarks - remember to check out the checkbox on the popup
    $(this).attr('rel', 'sidebar');
    //set the appropriate attributes
    $(this).attr('href', url);
    $(this).attr('title', name);

    //add bookmark:
    //  window.sidebar.addPanel(name, url, '');
    //  window.sidebar.addPanel(url, name, '');
    window.sidebar.addPanel('', '', '');
  } 
  else if (window.external) { // IE Favorite
        window.external.addFavorite(url, name);
  } 
  return;
});

html:

  <a id="bookmarkme" href="#" title="bookmark this page">Bookmark This Page</a>

<a href="javascript:window.external.addFavorite('http://tiny.cc/snir','snir-site')">..</a> Internet Explorerでは、「addFavorite」:と「AddFavorite」:の間に違いがあり ます<span onclick="window.external.AddFavorite(location.href, document.title);">..</span>

ここでの例:http ://www.yourhtmlsource.com/javascript/addtofavorites.html

重要なのは、Chromeではjs( aspnet-i) を使用してブックマークを追加できないことです:http: //www.codeproject.com/Questions/452899/How-to-add-bookmark-in-Google-Chrome-Opera-and- Saf

于 2015-09-01T14:01:18.380 に答える