Cross-Browser Add to Favorite Button

I really do not know if anybody use a favorite button on their websites. I know that social media buttons plugins are widely used and many on this plugins are also supports add to favorite button.

So, why I want to write a post about it? Because still a lot of my clients ask me to install the button on their websites.

And there is a cross-browser solution.

First of all let me show you the bookmark button HTML:

<a href="javascript:void(0)" onClick="return rudr_favorite(this);">Add to favorites</a>

The button JavaScript:

function rudr_favorite(a) {
	pageTitle=document.title;
	pageURL=document.location;
	try {
		// Internet Explorer solution
		eval("window.external.AddFa-vorite(pageURL, pageTitle)".replace(/-/g,''));
	}
	catch (e) {
		try {
			// Mozilla Firefox solution
			window.sidebar.addPanel(pageTitle, pageURL, "");
		}
		catch (e) {
			// Opera solution
			if (typeof(opera)=="object") {
				a.rel="sidebar";
				a.title=pageTitle;
				a.url=pageURL;
				return true;
			} else {
				// The rest browsers (i.e Chrome, Safari)
				alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 'Cmd' : 'Ctrl') + '+D to bookmark this page.');
			}
		}
	}
	return false;
}
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on Twitter