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

/2 comments

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

Related Posts

Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WordPress?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Comments — 2

Leave a comment

php js HTML CSS Code