Alex Rivera | Logout

Add to browser favorites/bookmarks from JavaScript but for all browsers (mine doesn't work in Chrome)?

Asked 2009-06-14T13:31:34.137
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');
}
Edit
Report

1 Answer

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 answered 2011-03-07T00:39:23.250

Your Answer