Search code examples
javascriptinternet-explorer-6internet-explorer-8favorites

Add to favorites in JavaScript for IE6+, FF and Safari


I tried some "add to favorties" JavaScript scripts.. With IE8, I get an "access denied" (is that even possible to add a bookmark via JS with IE8?) and it just doesn't work with IE6... Anybody has a good script that works on most browsers?

Thanks!


Solution

  • Both IE6 and IE8 will need the users to press CTRL+D to add the website to the favourites.

    Edit: Sorry, I run into a brain malfunction and mixed some words out.

    Actually, IE8 allows javascript to manage the favourites.

    To be more precise, and if you use jquery on your website, here's an example :

        $("a.bookmark").click(function(e) {
                if ($.browser.opera == false) {
                    e.preventDefault();
                    var url = this.href;  
                    var title = this.title;
    
                    if ($.browser.mozilla == true) {
                        window.sidebar.addPanel(title, url, '');
                        return false;
                    } else if($.browser.msie == true) {  
                        window.external.AddFavorite( url, title);
                        return false;
                    } else {
                        alert('Please use CTRL + D to bookmark this website.');
                    }
    
    
        }
    });
    

    Note: the "a.bookmark" is required to work with opera, since it recognizes .bookmark class in anchor tags and executes the bookmark funcion on click.

    It supports IE7 & 8, Firefox 2 & 3, and Opera 9 (at least) .. Safari isn't supported, and IE6 I couldn't test it here, sorry.