Bookmark / Add to favorites with JavaScript in IE, Firefox and Opera

December 14th, 2009 by Alex Leave a reply »

Many people come across this question while they markup the page. Here is the technique that will allow you to make a bookmark in most IE, Firefox and Opera browsers. First, set up a function in javascript:

function fav(elem) {
  var title = "Notes on web-design"; 
  var url = "http://graphicmaniacs.com";
 
	if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) {
		window.external.AddFavorite( url, title); }
	else if (window.opera && window.print) { 
    elem.setAttribute('href',url);
    elem.setAttribute('title',title);
    elem.setAttribute('rel','sidebar');
    elem.click();      
	}
}

Notice that i pass an element which called the function (variable elem) – this is a required stuff for Opera. Next step we add a link in our page:

<a href='javascript:fav(this)' title='Add to favorites'>Bookmark / Add to favorites</a>

Comments are closed.