Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Javascript to add a bookmark - Cross Browser
/**
*
* Add to bookmark
* Several tests are necessary in order for this "simple" action to work in most of the browsers
*
*/
// First, we define the element where the "Add to bookmark" action will trigger
var triggerBookmark = $(".js-bookmark"); // It must be an `a` tag
triggerBookmark.click(function() {
if (window.sidebar && window.sidebar.addPanel) { // Firefox <23
window.sidebar.addPanel(document.title,window.location.href,'');
} else if(window.external && ('AddFavorite' in window.external)) { // Internet Explorer
window.external.AddFavorite(location.href,document.title);
} else if(window.opera && window.print || window.sidebar && ! (window.sidebar instanceof Node)) { // Opera <15 and Firefox >23
/**
* For Firefox <23 and Opera <15, no need for JS to add to bookmarks
* The only thing needed is a `title` and a `rel="sidebar"`
* To ensure that the bookmarked URL doesn't have a complementary `#` from our trigger's href
* we force the current URL
*/
triggerBookmark.attr('rel', 'sidebar').attr('title', document.title).attr('href', window.location.href);
return true;
} else { // For the other browsers (mainly WebKit) we use a simple alert to inform users that they can add to bookmarks with ctrl+D/cmd+D
alert('You can add this page to your bookmarks by pressing ' + (navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? 'Command/Cmd' : 'CTRL') + ' + D on your keyboard.');
}
// If you have something in the `href` of your trigger
return false;
});
@romanbobrik

This comment has been minimized.

Copy link

@romanbobrik romanbobrik commented Feb 15, 2016

For Firefox <23 and Opera <15, no need for JS to add to bookmarks
The only thing needed is a title and a rel="sidebar"

triggerBookmark.attr('rel', 'sidebar').attr('title', document.title);
return true;

"href" attribute is necessary too (tested in FF 44.0.1)

@oilvier

This comment has been minimized.

Copy link
Owner Author

@oilvier oilvier commented Apr 11, 2017

@romanbobrik It works if your trigger element has an "href" like "#". The only drawback is that the saved URL is "http://url.com#".

So you're right, I've added the href in the script.

Thanks for your feedback !

@Zorono

This comment has been minimized.

Copy link

@Zorono Zorono commented Aug 8, 2017

it doesn't work on latest version of google chrome Windows 7 it always returns this alert('You can add this page to your bookmarks by pressing CTRL + D on your keyboard.');

Test Case: https://jsfiddle.net/mhmvbup2/1/

@toniengelhardt

This comment has been minimized.

Copy link

@toniengelhardt toniengelhardt commented Nov 19, 2017

Would be cool if there would be a different message on mobile devices.

@3rabii3

This comment has been minimized.

Copy link

@3rabii3 3rabii3 commented Mar 29, 2021

waist of time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment