JavaScript Code to Add Bookmarks/Favorites

We can add a link in the web pages which allows users to add the web page in the Favorites/Bookmarks.

To achieve this we need to write a JavaScript function, say "AddToFavorites", and add a hyperlink. And call the JavaScript function "AddToFavorites" by clicking the hyperlink.

Use the following code snippet to add the hyperlink to your web page:

<a href="#" onmousedown="AddToFavorites('google.com','http://www.google.co.in')" >Add to Favorites</a>

And the JavaScript function "AddToFavorites" can be defined as below:

function AddToFavorites(siteTitle, siteURL)

{

    if (window.sidebar)

    {

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

    }

    else if(document.all)

    {

        window.external.AddFavorite(siteURL, siteTitle);

    }

    else if(window.opera && window.print)

    {

        return true;

    }

}

Ebook Download
View all
Learn
View all