“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
Categories: jQuery
-
Tahir Yasin
-
http://www.mywebsitedesignersblog.com Andy Newby
-
Alexei Serov
-
http://www.mywebsitedesignersblog.com Andy Newby



