Improving the Delicious Bookmark Button with jQuery

Download Files ↓

Show Notes

The default Delicious Bookmark Button is obtrusive and broken when you switch off JavaScript. We show you how to improve and build an unobtrusive one with jQuery.


What you'll learn

  • How to build an unobtrusive JavaScript Delicious Bookmark Button with jQuery
  • How to url encode your links and titles for Delicious in Ruby on Rails and PHP

Welcome to the Improving the Delicious Bookmark Button with jQuery screencast. We’re going to assume you have a basic understanding of jQuery. If you’re new to jQuery we recommend watching our free introduction screencast on jQuery. While we’ll be specifically talking about Delicious here, the techniques can be used in other scenarios.

Sometimes we want to offer users the ability to bookmark pages or articles on Delicious.

Improving Delicious’ Code

Delicious provides “Bookmark this on Delicious” buttons on their website.

However their code is in-line and obtrusive. When JavaScript is switched off you can’t bookmark the page easily. Let’s fix that!

Download the zip file under this video on to follow along.

Here we have two “articles”, titled Introduction to jQuery, and Using AJAX with jQuery, and some lorem ipsum text. This is what you’d expect on a typical blog-like site when visiting their homepage. Our code has to work in this type of environment as well as on the article pages.

Included in the code are two of the “Bookmark this on Delicious” links, but they’re both the same. Also, if you were to click these links now, they would bookmark the same page, as it uses JavaScript to determine the path from the location.

First, let’s make this code unobtrusive.

So in our script tag, after jQuery is included, let’s select the anchors that have the class .delicious. Let’s move the code from the onclick attribute to the click event handler.

Let’s add className="delicious" to our Delicious links, as they don’t have a class at the moment.


Now let’s figure out what’s going on in this The code provided by Delicious uses the encodeURIComponent method to encode the URL and title of the article. We’d need to do this on the server.

In Ruby on Rails when outputting the link you’d do something like this:

Or in PHP:

You’ll notice we’ve included a commented-out static URL before each of the Delicious links, which includes the URL-encoded parameters. Let’s now replace the current Delicous URLs with these new URLs, which will function correctly if JavaScript is turned off.

Now, let’s alter our jQuery code to drop the JavaScript- generated URL, and replace it with this.href.

And that’s it!

When we open up our browser and click on these two links, we see that the correct URLs are being bookmarked, and as a bonus, when JavaScript is switched off, we can still bookmark the pages in an easy way.

We’re going to be covering more APIs for various web services and social networks in upcoming episodes, including an episode extending what we’ve done here by adding a Delicious bookmark count for each article.

Thanks for watching! Subscribe to our RSS feed, follow us on Twitter, and please leave any questions, comments or suggestions for new screencasts in the comments below. If you like our videos, and think your friends, followers or colleagues would benefit from seeing them, please feel free share via any of the links below the video. We really appreciate your support.

See you next time!