Add address bookmarks via JavaScript

0

Giving a search in Google (and here) I saw that it was possible some time ago to add addresses to browser bookmarks in JavaScript, but the methods used for this have been discontinued and I would like to know if there are new methods or not more possible to do it?

    
asked by anonymous 19.01.2018 / 03:25

1 answer

1

I use the code I found at this link: link

The example uses jQuery . I have tested again now that I am answering this question to confirm compatibility, and it works on IE 11, Firefox 51 and Chrome 63:

$('#addFavorito').click(function(e) {
    var bookmarkURL = window.location.href;
    var bookmarkTitle = document.title;

    if ('addToHomescreen' in window && addToHomescreen.isCompatible) {
      // Mobile browsers
      addToHomescreen({ autostart: false, startDelay: 0 }).show(true);
    } else if (window.sidebar && window.sidebar.addPanel) {
      // Firefox <=22
      window.sidebar.addPanel(bookmarkTitle, bookmarkURL, '');
    } else if ((window.sidebar && /Firefox/i.test(navigator.userAgent)) || (window.opera && window.print)) {
      // Firefox 23+ and Opera <=14
      $(this).attr({
        href: bookmarkURL,
        title: bookmarkTitle,
        rel: 'sidebar'
      }).off(e);
      return true;
    } else if (window.external && ('AddFavorite' in window.external)) {
      // IE Favorites
      window.external.AddFavorite(bookmarkURL, bookmarkTitle);
    } else {
      // Other browsers (mainly WebKit & Blink - Safari, Chrome, Opera 15+)
      alert('Press ' + (/Mac/i.test(navigator.userAgent) ? 'Cmd' : 'Ctrl') + '+D to bookmark this page.');
    }

    return false;
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><buttonid="addFavorito">Adicionar Favorito</button>
    
19.01.2018 / 12:13