2

I have the following bookmark Javascript.

function bookmark(title, url) {
   if(document.all) { // ie
       window.external.AddFavorite(url, title);
   }
   else 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(); // this.title=document.title;
    }
}

AND HTML

<a href="javascript:bookmark('title of the page', 'http://www.domain.com');" class="bookmark" >

And the problem is that is working only in Internet Explorer. Is not working in firefox, opera, chrome. Also i heard that firefox have deprecated the function window.sidebar.addPanel, is there any way to fix all of this? PLEASE NO JQUERY.

Adrian
  • 2,273
  • 4
  • 38
  • 78
  • Every browser has functionality to add bookmarks integrated in its UI already – so why would you even want to duplicate that? – CBroe Oct 29 '13 at 10:45
  • @CBroe So i can tell to the user to remind him that he can bookmark the site. And to do it in a easy way, like in one click, with no shortcut keys... – Adrian Oct 29 '13 at 10:47
  • There are lots of **Related** questions that seem to be on topic. Don't any of them answer your question? – Barmar Oct 29 '13 at 11:02
  • @Barmar most of them are with Jquery and there were some with Javascript, but did not solved my problem. Tried a lot. – Adrian Oct 29 '13 at 11:03
  • http://stackoverflow.com/questions/10033215/add-to-favorites-button seems to have non-jQuery methods for most browsers. But they look the same as what you're doing, so I don't know why it doesn't work for you. – Barmar Oct 29 '13 at 11:14
  • @Barmar That is JQuery for all answers. And the last answers have window.sidebar.addPanel which had been deprecated from firefox. – Adrian Oct 29 '13 at 11:18
  • The only jQuery in the answers is in the event binding, not in how it adds the bookmark. – Barmar Oct 29 '13 at 11:33
  • @Barmar well i am kind of noob, but id it contains Jquery i can't implemented because i don't have and don't want to add the jquery library on my site. It takes too much space and i have my site with 99% A+ on gt metrix, i am kind of a page speed freak. – Adrian Oct 29 '13 at 12:00
  • You don't need jQuery to copy the parts that set the bookmark. They work the same no matter how you bind the handler. – Barmar Oct 29 '13 at 12:03
  • @Barmar Could you please help me with a final answer. – Adrian Oct 29 '13 at 12:03

2 Answers2

3

Here's how to use the answer from How do I add an "Add to Favorites" button or link on my website? without the jQuery event binding.

function bookmark(title, href) {
    if (window.sidebar && window.sidebar.addPanel) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title,href,'');
    } else if(window.external && ('AddFavorite' in window.external)) { // IE Favorite
        window.external.AddFavorite(href,title); 
    } else if(window.opera && window.print) { // Opera Hotlist
        this.title=title;
        return true;
    } else { // webkit - safari/chrome
        alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');
    }
}
Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

There are some problem with the above solution.

   window.sidebar.addPanel("", "","");

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

Add "a" tag as shown below

<a id="BookmarkMe" href="">Bookmark</a>

And used below Jquery

$(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.');
                }
            });
        });
rgb
  • 143
  • 3
  • 15