80

Is there any cross-browser bookmark/add to favorites using JavaScript.

Searched for some list but none is working. Can you please suggest any?

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
Aakash Chakravarthy
  • 10,523
  • 18
  • 61
  • 78
  • 18
    To be honest: if a user needs a button to bookmark the site chances are he/she does not know how bookmarks work. Users who normally use bookmarks know that you can press CTRL+D, drag the little site icon on the bookmark bar, use the menu etc etc.. I find that putting a "bookmark us!" button seems a bit *retro*, so to say. Just make a good website with contents that pushes the user to bookmark it and you won't need any button. (Just my 2¢ on this type of situations, then it's you who have to decide what buttons to put on your website!) – nico Jun 12 '10 at 06:41
  • 6
    I am not putting a button in my website ! I am creating a wordpress plugin. Users who install it get a button to bookmark the current page like the "sociable" plugin – Aakash Chakravarthy Jun 12 '10 at 10:00
  • I'm thinking no. Bookmarks/favorites should be under the control of the user, imagine if any site you visited could insert itself into your bookmarks with just some javascript. – bwarner Jun 11 '10 at 17:18
  • I am not asking for automatic bookmarking. A button when clicked should bookmark the current page. I saw many scripts but none were cross browser support – Aakash Chakravarthy Jun 11 '10 at 17:21
  • OK, so you're asking for something that prompts the user whether they want to add your site to bookmarks. That's a bit different... – bwarner Jun 11 '10 at 17:27
  • Cmon, the OP is clearly NOT asking about automatic bookmarking. – Phil Nov 21 '17 at 14:10

3 Answers3

56

jQuery Version

JavaScript (modified from a script I found on someone's site - I just can't find the site again, so I can't give the person credit):

$(document).ready(function() {
  $("#bookmarkme").click(function() {
    if (window.sidebar) { // Mozilla Firefox Bookmark
      window.sidebar.addPanel(location.href,document.title,"");
    } else if(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;
    }
  });
});

HTML:

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

IE will show an error if you don't run it off a server (it doesn't allow JavaScript bookmarks via JavaScript when viewing it as a file://...).

jg6
  • 318
  • 2
  • 12
Gert Grenander
  • 16,866
  • 6
  • 40
  • 43
13
function bookmark(title, url) {
  if (window.sidebar) { 
    // Firefox
    window.sidebar.addPanel(title, url, '');
  } 
  else if (window.opera && window.print) 
  { 
    // Opera
    var elem = document.createElement('a');
    elem.setAttribute('href', url);
    elem.setAttribute('title', title);
    elem.setAttribute('rel', 'sidebar');
    elem.click(); //this.title=document.title;
  } 
  else if (document.all) 
  { 
    // ie
    window.external.AddFavorite(url, title);
  }
}

I used this & works great in IE, FF, Netscape. Chrome, Opera and safari do not support it!

adiga
  • 34,372
  • 9
  • 61
  • 83
Prashant Patil
  • 131
  • 1
  • 2
3

How about using a drop-in solution like ShareThis or AddThis? They have similar functionality, so it's quite possible they already solved the problem.

AddThis's code has a huge if/else browser version fork for saving favorites, though, with most branches ending in prompting the user to manually add the favorite themselves, so I am thinking that no such pure JavaScript implementation exists.

Otherwise, if you only need to support IE and Firefox, you have IE's window.externalAddFavorite( ) and Mozilla's window.sidebar.addPanel( ).

ajm
  • 19,795
  • 3
  • 32
  • 37