Bookmark

Asked

Viewed 1,680 times

2

I have a code for "add to favorites"

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

Only it is not working in any browser. : s Someone could see the problem?

2 answers

5


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

$('#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>
<a id="bookmark-this" href="#" title="Bookmark This Page">Bookmark This Page</a>

How to Add a Cross-Browser Bookmarker

1

Probably because you’re not including the jQuery before calling this code. The excerpt $(function(){ ... }); is equivalent to a function call ready(). 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.