Add to favorites

2

I have a "add to favorites" code

<script type="text/javascript">
    $(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>

But it is not working in any browser. :s Could anyone see the problem?

    
asked by anonymous 02.09.2015 / 00:36

2 answers

5

Hendy Tarnando has created an example of how to add bookmarks, it is compatible with all browsers, eg:

$('#bookmark-this').click(function(e) {
  var bookmarkURL = window.location.href;
  var bookmarkTitle = document.title;
  var triggerDefault = false;

  if (window.sidebar && window.sidebar.addPanel) {
    // Firefox version < 23
    window.sidebar.addPanel(bookmarkTitle, bookmarkURL, '');
  } else if ((window.sidebar && (navigator.userAgent.toLowerCase().indexOf('firefox') > -1)) || (window.opera && window.print)) {
    // Firefox version >= 23 and Opera Hotlist
    var $this = $(this);
    $this.attr('href', bookmarkURL);
    $this.attr('title', bookmarkTitle);
    $this.attr('rel', 'sidebar');
    $this.off(e);
    triggerDefault = true;
  } else if (window.external && ('AddFavorite' in window.external)) {
    // IE Favorite
    window.external.AddFavorite(bookmarkURL, bookmarkTitle);
  } else {
    // WebKit - Safari/Chrome
    alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 'Cmd' : 'Ctrl') + '+D to bookmark this page.');
  }

  return triggerDefault;
});
#bookmark-this {
  padding: 5px 10px;
  font-size: 12px;
  background: #f0ad4e;
  color: #fff;
  border: 1px solid #eea236;
  border-radius: 4px;
  text-decoration: none;
  text-shadow: 0 -1px 0 rgba(0, 0, 0, .2);
  -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
#bookmark-this:hover {
  background: #ec971f;
  border: 1px solid #d58512;
  text-decoration: none;
}
#bookmark-this:active {
  border: 1px solid #d58512;
  -webkit-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.2);
  box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.2);
  background: #ec971f;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><aid="bookmark-this" href="#" title="Bookmark This Page">Bookmark This Page</a>

How to Add a Cross-Browser Bookmarker

    
02.09.2015 / 01:10
1

You probably will not be including jQuery before calling this code. The $(function(){ ... }); snippet is equivalent to a ready() call. Apparently that's the only problem:

<a id="bookmarkme" href="#">Adicionar aos favoritos</a>

<!-- Incluindo o jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>

<!-- Chamando o seu código -->
<script>
  $(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>
    
02.09.2015 / 01:13