Friday, April 20, 2012

Adding URLs into Favorites/Bookmarks with Javascript


In one of a requirement, I had a link on my page, which on clicked, should add the URL of the page to the Favorites in IE and Bookmarks in Firefox/Safari etc.
So here is my script which will add the passed on URL into the favorites of the browser.
function addToFavoritesSSO(text, msg, returnUrl) {
        var link = returnUrl;
        var browserIndex = navigator.userAgent.indexOf('Safari');                   
        try {

            if (window.sidebar) {
                window.sidebar.addPanel(text, link, "");
            }
            else if (window.external) {
                window.external.AddFavorite(link, text);
            }
            else if (browserIndex != -1) {
                alert('Please press CTRL+D (or Command+D for macs) to bookmark this
page.');
            }
            else {
                alert('Please press CTRL+D (or Command+D for macs) to bookmark this
page.');
            }
        }
        catch (e) {
            alert(msg);
        }
    } 

One of the tough tasks is to add the bookmark in safari and chrome. Since both of these browsers don’t allow accessing the bookmarks programmatically, I’m just showing the shortcut keys to add the URL to bookmark.
I’ll update the function, once I find the solution to add bookmark through JavaScript for Safari and Chrome.
Cheers
Ankit

No comments:

Post a Comment