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 […]

#JavaScript  /   2

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;
}

Only the best of WordPress

Subscribe to this weekly newsletter to receive the latest blog posts by email.I respect your privacy. Your email is safe with me.

Comments 2

Leave your question or feedback

phpjsHTMLCSSSQLCode
Please, enter a comment
Please, enter a name
Incorrect email