“Bookmark this page” using jQuery, for IE and Firefox

May 11th, 2011

I was recently tasked with coming up with a decent function, which will let the majority of people add a “bookmark this site” link on their websites. It was amazing how long winded it was to find a working solution!

I eventually came across a plugin for jQuery, which almost let you do this out of the box – http://calisza.wordpress.com/2008/11/03/javascript-jquery-bookmark-script/

The problem (as usual!) was IE not playing ball!

To get this plugin working, I had to make a few changes to it. First of all, down the plugin from the above link – and then change the code in jqbookmarkjs, to :

$(document).ready(function(){
	// add a "rel" attrib if Opera 7+
	if(window.opera) {
		if ($("a.jqbookmark").attr("rel") != ""){
			$("a.jqbookmark").attr("rel","sidebar");
		}
	}

	$("a.jqbookmark").click(function(event){
		event.preventDefault();
		var url = this.href;
		var title = this.title;

		if ($.browser.mozilla ) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( $.browser.msie ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; // do nothing
		} else {
			 alert('Unfortunately, this browser does not support the requested action,'
			 + ' please bookmark this page manually.');
		}

	});
});

I tweaked the code to make it use $.browser.mozilla and $.browser.msie , which seems to work well in both firefox and IE 8 (not got any copies of older versions, so I’m afraid I can’t test it with those :( )

To add a “bookmark this page” link, you would simply do:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript" src="/static/jqbookmark.js"></script>

…and then in your area, add a link. Something like:

<a href="http://www.mywebsitedesignersblog.com" title="My Site" class="jqbookmark">Make MyWebsiteDesignsBlog your home page</a>

Please let me know how you get on with it :)

Andy

Andy

I’ve been working as a Web-Design / Coder for many years now, and thought it was finally time for me to give back some of the stuff I’ve learned over the years. I started this blog in November 2010, and have already added quite a few varying articles to the site. I’m always looking for new suggestions, so please visit the forum, or shoot me an email with suggestions!

WebsiteMore Posts

Categories: jQuery

  • Tahir Yasin

    I am in search of exact functionality for Google Chrome but still unsuccessful, can you please help me to create a bookmark link which works in Chrome also.

    Any help will be highly appreciated.

    Thanks

    • http://www.mywebsitedesignersblog.com Andy Newby

      Hi – do you have an example where it doesn’t work?

  • Alexei Serov

    I see and read on the internet that in the Firefox window.sidebar.addPanel(title, url,”"); opens the bookmark in the sidebar . What can I do to open it in the main window?

  • http://www.mywebsitedesignersblog.com Andy Newby

    As far as I know, it should open in a pop-up box FireFox (at least it does for me). You may find it easier using my jQuery Plugins which can be found here: http://www.mywebsitedesignersblog.com/2012/02/jquery-add-bookmark-plugin-abookmark/

Related Posts: