1

I have a word as a custom "Add to Favorites" button. Clicking on the word link should add the website's URL to the user browser's favorites (bookmarks). This should work for all browsers, IE7+, FF, Opera, Chrome. As my code is working but i think i lack of something, it doesn't when clicked it will auto bookmarks instead of clicking the dialog alert. Kindly advise

Here my code :

$(function(){
    function AddFavorite(sURL, sTitle)
    {
        try
        {
            window.external.addFavorite(sURL, sTitle);
        }
        catch (e)
        {
            try
            {
                window.sidebar.addPanel(sTitle, sURL, "");
            }
            catch (e)
            {
                alert("请使用Ctrl+D进行添加");
            }
        }
    }
    $('div.link_r').find('a').on('click',function(){
        AddFavorite(location.herf,'新蔡');
        return false;
    });

HTML :

  <div class="link_r">
       <a  href="#">收藏此页面,开奖即时查看</a>
  </div>
Morgan Ng
  • 813
  • 2
  • 10
  • 22

2 Answers2

2

$(function() {
  function AddFavorite(sURL, sTitle) {
    if (/firefox/i.test(navigator.userAgent)) {
      return false; //firefox work with attr "rel=sidebar"
    } else if (window.external && window.external.addFavorite) {
      window.external.addFavorite(sURL, sTitle);
      return true;
    } else if (window.sidebar && window.sidebar.addPanel) {
      window.sidebar.addPanel(sTitle, sURL, "");
      return true;
    } else {
      var touch = (navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL');
      alert('Press ' + touch + ' + D to bookmark this page.');
      return false;
    }
  }
  $("div.link_r a").attr("rel", "sidebar").click(function() {
    return !AddFavorite(window.location.href, $(this).attr("title"));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="link_r">
  <a href="#" title="新蔡">收藏此页面,开奖即时查看</a>
</div>

See : How do I add an "Add to Favorites" button or link on my website?

Community
  • 1
  • 1
user2226755
  • 12,494
  • 5
  • 50
  • 73
0

This works on some browsers :

$('#bookmark_me').click(function(e){
  e.preventDefault();
  var bookmarkURL = this.href;
  var bookmarkTitle = this.title;
  try {
    if (window.sidebar) { // moz
      window.sidebar.addPanel(bookmarkTitle, bookmarkURL, "");
    } else if (window.external || document.all) { // ie
      window.external.AddFavorite(bookmarkURL, bookmarkTitle);
    } else if (window.opera) { // duh
      $('a#bookmark').attr('href',bookmarkURL);
      $('a#bookmark').attr('title',bookmarkTitle);
      $('a#bookmark').attr('rel','sidebar');
    }
  } catch (err) { // catch all incl webkit
    alert('Sorry. Your browser does not support this bookmark action.     Please bookmark this page manually.');
  }
});

DEMO

mrid
  • 5,782
  • 5
  • 28
  • 71