A while back I had to create a webpage that allowed users to bookmark a certain website. Turns out only IE supports bookmarking a webpage. And there’s a hell of a lot of bad code out there that says otherwise but doesn’t actually work for any other browsers.
So now I’m adding Bookmark.js to the mix. It is a teeny tiny JS object that insulates you from figuring out if the user’s browser can bookmark your page. It can also tell you the correct keystroke to bookmark your page, which you can then alert/show to your users.
Example:
<script type="text/javascript" src="bookmark.js"></script>
<script type="text/javascript">
Bookmark.canBookmark(); //True if you can, false if not
Bookmark.getKeystroke(); //"Command/Cmd + D" if Mac, "CTRL + D" if Win
Bookmark.bookmarkPage('http://www.ryandoherty.net');
//True if attempted to bookmark, false if not possible.
</script>
Tested in IE 6 & 7, FF 2 & 3, Safari 2 & 3 and Opera 9.
More Info
If you search around for bookmarking scripts, you’ll find a lot of code that describes how to do it in most browsers. As usual with lots of Javascript on the net, it’s bad code and wrong.
IE has the bookmark function, window.external.AddFavorite. Firefox has the function window.sidebar.addPanel which is very similar and will add an item to your bookmarks, but will open in a sidebar panel!.
For Opera, lots of people have written about dynamically creating a link, setting the ‘rel’ attribute to ’sidebar’ and firing the click event. I’ve tested this and can’t get it to work in the latest version of Opera. Safari doesn’t have anything available either.
This is a simple feature that I’d like implemented in all major browsers. Shouldn’t be too hard, right?