Wednesday, January 9, 2008

How to add to Bookmark in IE/FF using javascript?

All the web developers would agree on one point that it has been a challenge to write a code which should behave almost identically in all the browsers. If not all the broweser it should behave consistently in Internet Explorer(IE)/Firefox(FF)/Opera. It takes a few hours to write a code which works on either IE or FF and takes another couple of hours to make that code generic enough so that it works both on IE and FF. I too was caught with this bug when I was trying to add an "Add this site to Bookmark" link to a webpage. The following code should do this desired task of adding the current website to the Bookmark of the browser:


function AddToBookmark()
{

title = "Webpage Title";

if (window.sidebar)
{ // Mozilla Firefox Bookmark
window.sidebar.addPanel(title, url,"");
}
else if( window.external )
{ // IE Favorite
window.external.AddFavorite( url, title);
}
else if(window.opera && window.print)
{ // Opera Hotlist return true;
}

}


The code might need a few settings to be changed on a Firefox browser to behave properly and not open the saved bookmark in the side pannel of the browser.

No comments: