2

I have some query in Add to favorites/Bookmarked web page using java script.I just go through in this Link and get this source code. It's been working perfectly for the last month. Yet now it will not work in any browser. Here is my code :

function CreateBookmarkLink(){
        var title = document.title;
        var url = document.location.href;

        if(window.sidebar){
            /* Mozilla Firefox Bookmark */
            window.sidebar.addPanel(title, url, "");
        }else if(window.external){
            /* IE Favorite */
            window.external.AddFavorite(url, title);
        }else if(window.opera && window.print) {
            /* Opera Hotlist */
            alert("Press Control + D to bookmark");
            return true;
        }else{
            /* Other */
            alert("Press Control + D to bookmark");
        }
 <a href="javascript:CreateBookmarkLink();">Add to Favorites/Bookmark</a>

It doesn't work in any browser any more and just displays:

TypeError: window.sidebar.addPanel is not a function
> window.sidebar.addPanel(title, url, "");

Any ideas how to solve it? I also need to add favorites in chrome browser. Any other idea to create bookmark for my website.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
user2711066
  • 169
  • 2
  • 2
  • 10

1 Answers1

7

Due to security reasons it is not possible to add a bookmark in Google Chrome using Javascript.

Alternatively, you could output a message for using the shortcut:

    $('#bookmarkme').click(function(){
        alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');
    }); 

Since window.sidebar.addPanel is deprecated and not a standard implementation (https://developer.mozilla.org/en-US/docs/Web/APIWwindow.sidebar), you may use the API to add bookmarks when creating an add-on (https://developer.mozilla.org/en-US/docs/Code_snippets/Bookmarks).

Nevertheless it should not be a high burden for users to add their favorite website as bookmark inside the browser.

rmunn
  • 34,942
  • 10
  • 74
  • 105