13

Is there a way to save the current page as a bookmark (through jQuery or otherwise) when a specific button is clicked?

conbask
  • 9,741
  • 16
  • 57
  • 94

4 Answers4

12
<script language="javascript" type="text/javascript">
$(document).ready(function(){
  $("a.jQueryBookmark").click(function(e){
    e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
    var bookmarkUrl = this.href;
    var bookmarkTitle = this.title;

    if (window.sidebar) { // For Mozilla Firefox Bookmark
        window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
    } else if( window.external || document.all) { // For IE Favorite
        window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
    } else if(window.opera) { // For Opera Browsers
        $("a.jQueryBookmark").attr("href",bookmarkUrl);
        $("a.jQueryBookmark").attr("title",bookmarkTitle);
        $("a.jQueryBookmark").attr("rel","sidebar");
    } else { // for other browsers which does not support
         alert('Your browser does not support this bookmark action');
         return false;
    }
  });
});
</script>

This Code is taken from Developersnippets!

/e:

Chrome does not support such actions, since the security level could be broken.

bastianwegge
  • 2,435
  • 1
  • 24
  • 30
  • 1
    How can I get it to work in Chrome? In Chrome the alert message doesn't even show... – conbask Apr 29 '11 at 07:17
  • 2
    To prevent throwing an error in Chrome you should use `else if(window.external && window.external.AddFavorite)` since `window.external` ist defined in Chrome, but not `window.external.AddFavorite`. – Johannes Klauß May 14 '13 at 08:45
9

Since Chrome does not support such action, a solution could be to check first if the browser in use it's Chrome and if so to alert the user that the bookmark function is not supported. Then for other cases the script provided on DevelopersSnippets works fine.

Example:

   $("a.bookmark").click(function(e){
    e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
    var bookmarkUrl = this.href;
    var bookmarkTitle = this.title;
    if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) { 
            alert("This function is not available in Google Chrome. Click the star symbol at the end of the address-bar or hit Ctrl-D (Command+D for Macs) to create a bookmark.");      
    }else if (window.sidebar) { // For Mozilla Firefox Bookmark
        window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
    } else if( window.external || document.all) { // For IE Favorite
        window.external.AddFavorite( bookmarkUrl, bookmarkTitle);          
    } else if(window.opera) { // For Opera Browsers
        $("a.bookmark").attr("href",bookmarkUrl);
        $("a.bookmark").attr("title",bookmarkTitle);
        $("a.bookmark").attr("rel","sidebar");
    } else { // for other browsers which does not support
         alert('Your browser does not support this bookmark action');
         return false;
    }
  });
Alessandro Incarnati
  • 7,018
  • 3
  • 38
  • 54
1

Try this:

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();
} 
else if(document.all)// ie
    window.external.AddFavorite(url, title);
}
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
1

I think jquery Bookmark plugin is what you are looking for . jBrowserBookmark allows you to add functionality to a site which allows a page to be added to the browsers boookmark list. This feature is supported by Internet Explorer, Firefox, Opera and Konqueror browsers.You can get it here

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Abdul Kader
  • 5,781
  • 4
  • 22
  • 40
  • Replaced old/dead link for another one, not sure if it's the same author but it's one of the first links when searching for this name. The plugin is just an encapsulation of the methods in the other answers. – brasofilo Aug 21 '18 at 21:52