27

Can anyone help, I am using the following for adding a bookmark to IE and Firefox but its not working in Chrome and I don't get my error msg saying "not supported" either..

Anybody know of a good script to support ALL browsers or at least to come back and tell me its not supported, I have access to jQuery - maybe there is some way to detect the browser

I am currently using this and it works for IE and Firefox but not chrome

if (window.sidebar) { // Mozilla Firefox
    window.sidebar.addPanel(name, url, "");
}
else if (window.external) { // IE
    window.external.AddFavorite(url, name);
}
else if (window.opera && window.print) {
    window.external.AddFavorite(url, name);
}
else {
    alert('not supported');
}
Volker E.
  • 5,911
  • 11
  • 47
  • 64
mark smith
  • 20,637
  • 47
  • 135
  • 187
  • I'm sorry.. do you mean to say you're trying to force the user's browser to add a bookmark? While a firefox or ie add-on could do what you suggest, a regular script on a page would most certainly not have access to such. – Jonathan Fingland Jun 14 '09 at 13:57
  • Actually, the IE method is widely used (often on "add to favorites" button), it opens the adding dialog. That's a bad practice, but it got really popular when Internet beginners (=> on IE) didn't know they could do it by themselves. – instanceof me Jun 14 '09 at 14:15
  • 3
    yeah, it's a dumb idea (like "back" links in pages). but clients often see it on other sites and want it. – SpliFF Jun 14 '09 at 15:24
  • 1
    Hmm.. I wonder if there is any real correlation between asking someone to bookmark your page and people actually doing it? it seems to me as useful as a button saying "like this page", as though clicking it somehow makes the page better. – SpliFF Jun 14 '09 at 15:26
  • 3
    Humorously, since SO has an `id="sidebar"`, the window.sidebar check succeeds...and then the addPanel call fails spectacularly. So, if you're going to use the above code, you should check to make sure `window.sidebar` is actually what you think it is. – Yahel Mar 21 '11 at 20:29
  • 2
    Why not have a note that says "use Ctrl+D to bookmark this page"? – NoBugs May 18 '12 at 16:17

6 Answers6

13

Sorry, but there's no cross-browser way to do this. Your FF example is broken as well: It won't create a regular bookmark, but a bookmark set to be opened in the sidebar. You'd have to use the bookmark-service to create an actual bookmark, but this'll fail due to security restrictions.

Christoph
  • 164,997
  • 36
  • 182
  • 240
  • yep me too.. thought it was a dumb idea but the client wants it.. but if it isn't possible I suppose i will have find something else :-) – mark smith Jun 14 '09 at 17:22
  • 6
    If this is only for an ill-informed client, then why care if it works in Chrome or not? – DA. Nov 24 '09 at 20:12
  • the FF situation has slightly improved: thanks to UI changes, it's now possible to mark the bookmark as regular when adding it, but by default it will still be opened in the sidebar – Christoph Jun 11 '10 at 19:10
  • 1
    This answer is outdated imo, see the script posted by FFish – Stephan Muller Feb 23 '11 at 10:07
  • @Stephan: I disagree - you're correct that it now works in FF (if you disregard the fact that users will have to uncheck the sidebar box to get a proper bookmark, which I already commented on), but in many other browsers, the script will just show an `alert()` telling the user to hit some key combination; as before, there still is no real cross-browser solution available. – Christoph Feb 23 '11 at 14:41
6

After discovering - like Edison! - a bunch of ways this doesn't work, I eventually came across this page that says adding bookmarks via JS is explicitly disabled in Chrome. Unfortunately it does not explain why.

Update: I was asked to expand this answer by another SO user...

My links and buttons for this function all have a class="addbookmark" associated with them. When the user agent is Chrome, I use some jQuery to disable the links and explain why:

<script type="text/javascript" src="/scripts/jquery-1.4.2.min.js"></script> 
<script type="text/javascript" src="/scripts/bookmark.js"></script> 
<script> 
    title='A Label for this Bookmark, ie title of this page'; // for example, not really generated this way... 

    $jQuery(document).ready(function(){ 
        // chrome does not permit addToFavorites() function by design 
        if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) { 
            $('.addbookmark').attr({ 
                title: 'This function is not available in Google Chrome. Click the star symbol at the end of the address-bar or hit Ctrl-D to create a bookmark.', 
                href: 'javascript:return false' 
            }) 
            .css({opacity: .25});       // dim the button/link 
        } 
    }); 
</script> 

And then elsewhere on the page:

 <td rowspan="2" class="noprint" style="width:24px;"> 
     <a class="addbookmark" title="Save a Bookmark for this page" 
        href="javascript:addToFavorites(location.href,title)"> 
        <img style="width:24px; height:24px; padding-top:2px;" src="/images/bookmark.gif"></a> 
 </td> 

... which is by no means perfect, but it seems one's options are fairly limited.

The version of jQuery isn't important, and it's up to you whether you want a local copy or hot-link to the google version. bookmark.js is pretty much exactly as per the OP's code:

$ cat /scripts/bookmark.js 
/* simple cross-browser script for adding a bookmark 
    source: http://stackoverflow.com/questions/992844/add-to-browser-favourites-bookmarks-from-javascript-but-for-all-browsers-mine-do 
*/ 
function addToFavorites(url, name) { 
    if (window.sidebar) { // Mozilla Firefox 
        window.sidebar.addPanel(name, url, ""); 
    } else if (window.external) { // IE 
        window.external.AddFavorite(url, name); 
    } else if (window.opera && window.print) { 
        window.external.AddFavorite(url, name); 
    } else { 
        alert("Sorry! Your browser doesn't appear to support this function."); 
    } 
} 

Hope that's useful.

RET
  • 9,100
  • 1
  • 28
  • 33
5

I just tested this script in:

Win

  • IE 6.0, IE 7.0, IE 8.0
  • Firefox 2.0, Firefox 3.6.3
  • Safari 3.1.2, Safari 3.2.3
  • Opera 9.00
  • Google Chrome 8.0

Mac

  • Firefox 3.6.13
  • Safari 5.0.1
  • Opera 11.0
  • Google Chrome 8.0

    /*
    * Copyright 2010 by GlamThumbs Team.
    *
    * How To Use The Script:
    * add to your page this code between inside head tags
    * <script type="text/javascript" src="ATBookmarkApp.js"></script> 
    * add anchor with void href like this: 
    * <a href="javascript:void(0)" onClick="return BookmarkApp.addBookmark(this)">bookmark us</a> 
    * 
    */
    
    ATBookmarkApp = function () {
        var isIEmac = false; /*@cc_on @if(@_jscript&&!(@_win32||@_win16)&& 
    (@_jscript_version<5.5)) isIEmac=true; @end @*/
        var isMSIE = (-[1,]) ? false : true;
        var cjTitle = document.title;
        var cjHref = location.href;
    
        function hotKeys() {
            var ua = navigator.userAgent.toLowerCase();
            var str = '';
            var isWebkit = (ua.indexOf('webkit') != - 1);
            var isMac = (ua.indexOf('mac') != - 1);
    
            if (ua.indexOf('konqueror') != - 1) {
                str = 'CTRL + B'; // Konqueror
            } else if (window.home || isWebkit || isIEmac || isMac) {
                str = (isMac ? 'Command/Cmd' : 'CTRL') + ' + D'; // Netscape, Safari, iCab, IE5/Mac
            }
            return ((str) ? 'Press ' + str + ' to bookmark this page.' : str);
        }
    
        function isIE8() {
            var rv = -1;
            if (navigator.appName == 'Microsoft Internet Explorer') {
                var ua = navigator.userAgent;
                var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
                if (re.exec(ua) != null) {
                    rv = parseFloat(RegExp.$1);
                }
            }
            if (rv > - 1) {
                if (rv >= 8.0) {
                    return true;
                }
            }
            return false;
        }
    
        function addBookmark(a) {
            try {
                if (typeof a == "object" && a.tagName.toLowerCase() == "a") {
                    a.style.cursor = 'pointer';
                    if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) {
                        window.sidebar.addPanel(cjTitle, cjHref, ""); // Gecko
                        return false;   
                    } else if (isMSIE && typeof window.external == "object") {
                        if (isIE8()) {
                            window.external.AddToFavoritesBar(cjHref, cjTitle); // IE 8                    
                        } else {
                            window.external.AddFavorite(cjHref, cjTitle); // IE <=7
                        }
                        return false;
                    } else if (window.opera) {
                        a.href = cjHref;
                        a.title = cjTitle;
                        a.rel = 'sidebar'; // Opera 7+
                        return true;
                    } else {
                        alert(hotKeys());
                    }
                } else {
                    throw "Error occured.\r\nNote, only A tagname is allowed!";
                }
            } catch (err) {
                alert(err);
            }
    
        }
    
        return {
            addBookmark : addBookmark
        }
    }();
    
Null
  • 154
  • 1
  • 14
FFish
  • 10,964
  • 34
  • 95
  • 136
  • 4
    @FFish this will not work because you instruct BookmarkApp.addBookmark(this) usage but regarding to your script it must be ATBookmarkApp.addBookmark(this) please edit it. And please, what is that "GlamThumbs - Free Porn Video Galleries" title. Please use something else :) I was almost forgot it not changed and it will be really bad for visitors. – aiternal Feb 08 '11 at 14:29
  • I edited the answer above to actually call the right class, and I removed the GlamThumbs title (almost forgot to remove it myself just now). Apart from that, working like a charm! – Stephan Muller Feb 23 '11 at 10:05
  • @Stephan: I don't consider displaying a message box which tells the user to hit a key combination 'working like a charm'... – Christoph Feb 23 '11 at 14:46
  • And in IE9 I get an empty alert. Also you call the variable ATBookmarkApp, but on the link onclick handler you return BookmarkApp. – Spadar Shut Nov 29 '11 at 13:51
4

You can always alert the client to press ctr+D. This is universal across all browsers. It's tacky, but just as useful to the client.

CTRL + D -- for Windows
CMD + D -- for Mac

a_r_m
  • 17
  • 4
  • Actually for Opera versions before 9: It's CTRL + T, on Konqueror: CTRL + B. Than you could also specify if it's 'CTRL' or 'CMD/Command', Win vs Mac. – FFish Jan 03 '11 at 09:21
  • 2
    arlo: Why add a starter button to your car when you can "just" crank it up, as we always do (in 1910) ? – Didier Levy Sep 22 '11 at 19:43
2

I couldnt get the above example to work. Anyway the answer to the original question 'its not working in CHROME and i don't get my error msg saying "not supported" either..' is due to the line

else if (window.external) { // IE 

chrome actually passes this test and then obiously fails to add a bookmark. I changed this line to

else if(window.external && !window.chrome)  // IE

and now you get the 'not supported' message. I actually removed this message and called the function hotKeys() to get a more meaningful alert. I had to make a few changes to get that to work

function showHotKeys() 
{ 
var ua = navigator.userAgent.toLowerCase(); 
var str = ''; 
var isWebkit = (ua.indexOf('webkit') != - 1); 
var isMac = (ua.indexOf('mac') != - 1); 

if (ua.indexOf('konqueror') != - 1) { 
    str = 'CTRL + B'; // Konqueror 
} else if (window.home || isWebkit || isMac) { 
    str = (isMac ? 'Command/Cmd' : 'CTRL') + ' + D'; // Netscape, Safari, iCab
} 
return ((str) ? 'Press ' + str + ' to bookmark this page.' : str); 
} 
Tony K
  • 21
  • 1
0

My approach with help of jQuery.
Tested in IE 6-8, Fx 1-25, Opera 7-14. Degrades gracefully in Chrome, Saf.

CSS:

.no-js .link-bookmark {
   display: none;
}

JS:

/* ... Bookmark current page ... */
var $favLink = $('.link-bookmark');

if ( window.sidebar || 'AddFavorite' in window.external || window.opera ) {
    $favLink.show();
}

// add a 'rel' attrib if Op 7+ && Fx >= 23
if ( window.opera || window.sidebar ) {
    var $favLinkAttrRel = $favLink.attr('rel');
    if ( typeof $favLinkAttrRel !== "undefined" && $favLinkAttrRel !== false ) { // don't overwrite the rel attr if already set
        $favLink.attr('rel', 'sidebar');
    }
}

$favLink.click(function( event ) {
//event.preventDefault(); // prevent the anchor tag from sending the user off to the link
var url = this.href;
var $title = $('title').text();

// IE Favorite
if ( 'AddFavorite' in window.external ) {
    event.preventDefault();
    window.external.AddFavorite(url, $title);
}
// Fx <23 Bookmark, 'addPanel' not available from v23 on any more.
else if ( 'addPanel' in window.sidebar ) {
    event.preventDefault();
    window.sidebar.addPanel($title, url, '');
}
// Op 7+ && Fx >= 23
else if ( window.opera || window.sidebar ) {
    $favLink.attr('title', $title);
    return true; // do nothing - the rel="sidebar" should do the trick
}
// for Saf, Konq etc - browsers who do not support bookmarking scripts
else {
    event.preventDefault();
    alert('Your browser doesn\'t support the bookmark functionality,'
    + 'please add this page to your bookmarks manually.');
} 
});
Volker E.
  • 5,911
  • 11
  • 47
  • 64