Add to Bookmark/Favourites : Javascript

As we know, every browsers comes with a functionality to Bookmark/Favourite the current page we are visiting. But most of the users not even think about this feature. Sometimes we can set a link or button or any other control in your mind, in order to remind the user to add the page in their Bookmark/Favourite list. Here goes a simple javascript function that will add the current page in to the users Bookmark/Favourite list.

Use this javascript inside your <Head> tag.


<script language=”javascript” type=”text/javascript”>

function AddFavorite_onclick()
{
    BookmarkTitle = “Silent Killer”;
    BookmarkURL = “https://binzthomas.wordpress.com/&#8221;;

if (window.sidebar)
{ // Bookmark for Mozilla Firefox
    window.sidebar.addPanel(BookmarkTitle, BookmarkURL,””);
}
else if( window.external )
{ // IE’s Favorite
    window.external.AddFavorite( BookmarkURL, BookmarkTitle);
}
else if(window.opera && window.print)
{ // Opera Hotlist
    var element = document.createElement(“a”);
    element.setAttribute(“href”, BookmarkURL);
    element.setAttribute(“title”, BookmarkTitle);
    element.setAttribute(“rel”, “sidebar”);
    element.click();
}
}
</script>

Now add the <a> tag inside the <body> . We are going to add the link text dynamically according to the browser we are using. That means, in IE we have Favourites and in Mozilla it is Bookmarks. So the link will display Add to Favorites in IE and Bookmark Page in Mozilla.

<a href=”javascript:AddFavorite_onclick() ” id=’link’ ></a>


<script language=”javascript” type=”text/javascript”>

if (document.all)
{   
    document.getElementById(‘link’).innerHTML=”Add to Favorites”;
}
else
if (window.sidebar) 

    document.getElementById(‘link’).innerHTML=”Bookmark Page”;   
}
else
if (window.opera && window.print)

    document.getElementById(‘link’).innerHTML=”Add Bookmark”; 
}

</script>