Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
How to add bookmark in jquery? Here I am write a code for bookmark but it won't support in chrome browser.


C#
$("a#Bookmark").click(function () {
               var bookmarkUrl = this.href;
               var bookmarkTitle = this.title;
               //alert(bookmarkurl);
               if ($.browser.mozilla) // For Mozilla Firefox Bookmark
               {
                   window.sidebar.addPanel(bookmarkTitle, bookmarkUrl, "");
               }
               else if ($.browser.msie || $.browser.webkit) // For IE Favorite
               {
                   window.external.AddFavorite(bookmarkUrl, bookmarkTitle);
               }
               else if ($.browser.opera) // For Opera Browsers
               {
                   $(this).attr("href", bookmarkUrl);
                   $(this).attr("title", bookmarkTitle);
                   $(this).attr("rel", "sidebar");
                   $(this).click();
               }
               else // for other browsers which does not support
               {
                   alert('Please hold CTRL+D and click the link to bookmark it in your browser.');
               }
               return false;
           });
Posted
Updated 5-Aug-16 8:07am

Try this:-
JavaScript
$(document).ready(function(){
  $("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 (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;
    }
  });
});
 
Share this answer
 
v3
There are some problem with the above solution.
JavaScript
window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");

The above code may not work in all Mozilla Firefox versions.
So I wrote the bookmaking code as below. It working fine in all browsers except webkit - safari/chrome,

Added "a" tag as shown below
HTML
<a id="BookmarkMe" href="">Bookmark</a>

And used below Jquery
JavaScript
$(function () {
    $('#BookmarkMe').click(function (e) {
        var bTitle = document.title, bUrl = window.location.href;
        if ($.browser.mozilla || $.browser.opera) { // Mozilla Firefox or Opera
            if (window.sidebar.addPanel) {
                e.preventDefault();
                window.sidebar.addPanel(bTitle, bUrl, "");
            }
            else {
                $('#BookmarkMe').attr("href", bUrl);
                $('#BookmarkMe').attr("title", bTitle);
                $('#BookmarkMe').attr("rel", "sidebar");
            }
        } else if ($.browser.msie) { // IE Favorite
            e.preventDefault();
            window.external.AddFavorite(bUrl, bTitle);
        } else { // webkit - safari/chrome
            e.preventDefault();
            alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');
        }
    });
});
 
Share this answer
 
Did not test in opera - but since the new Jquery does not have $browser - maybe we shall use:

$(document).ready(function()
{
var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
// var isMozilla = /firefox/.test(navigator.userAgent.toLowerCase());

$("a.jQueryBookmark").click(function(e)
{
e.preventDefault(); // this will prevent the anchor tag from taking the user off to the link

var bookmarkUrl = this.href;
var bookmarkTitle = this.title;

if((window.external || document.all) && !isChrome)
{ // 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;
}
});
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900