|
|
Want to know more about Packt's Article Network? Interested in contributing your article ideas? Please visit our FAQ for more information. See More BROWSE
All Titles WordPress Web Services SOA BPEL Web Graphics & Video Web Development RAW Portugues, Espanol, Italiano, French PHP/MySQL Oracle Open Source Networking & Telephony Moodle Microsoft & .NET Linux Servers jQuery Joomla! JBoss Java e-Learning e-Commerce Dynamics Drupal CRM Cookbook Content Management Beginner Guides Architecture and Analysis AJAX Future Titles Recently Published Titles In this article by Roshan Bhattarai, we learn how to build a widget similar to the Mybloglog widget using external JavaScript along with PHP. By the time we finish, you would see a popup image whenever you move the mouse cursor over the widget. In the later section, you can see an example for extracting image details from a database and using it in the example. See More |
Delicious Tagometer Widget
Background ConceptDelicious was founded by Joshua Schachter in 2003 and acquired by Yahoo in 2005. This website was formerly used to run in the domain http://del.icio.us hence known as del.icio.us. Now, this domain redirects to the domain http://delicious.com. This website got redesigned in July 2008 and the Delicious 2.0 went live with new domain, design and name. Delicious is probably one of the largest social bookmarking website in the WWW world for discovering, sharing and storing the interesting and useful URL on the Internet. When saving the URL, user can enter tags for the URL which is quite useful when you’ve to search the particular URL from many bookmarks. The number of saves of a particular URL in Delicious is one of the measurements for checking popularity of that URL. Delicious TagometerAs the name specifies, Delicious Tagometer is a badge which displays the tags as well count of the users who have saved the particular URL. Tagometer gets displayed in the web page which contains the code given below: <script src="http://static.delicious.com/js/blogbadge.js"></script> This is the new URL of the Tagometer badge from the Delicious. The URL for the Tagometer used to be different in the del.icio.us domain in past. For more information on future updates, you can check http://delicious.com/help. The delicious Tagometer looks as shown:
As you can easily guess, the above Tagometer is placed on a web page whose URL has not yet been saved in Delicious. Now let’s take a look at the Tagometer which is placed on a web page whose URL is saved by many users of Delicious.
In the above widget, the text “bookmark this on Delicious" is the hyperlink for saving the URL in delicious. To save an URL in Delicious, you have to provide the URL and give a Title on the http://delicious.com/save page. After clicking on the “bookmark this on Delicious” hyperlink on the Delicious Tagometer widget you can see the image of the web page on delicious.
The Delicious Tagometer widget also shows the list of tags which are used by the users of Delicious to save the URL. Each of these tags link to a tag specific page of Delicious. For example, an URL saved with tag JavaScript can be found on the page http://delicious.com/tag/javascript. And, the number is linked to the URL specific page on Delicious. For example, if you wish to view the users and their notes on the saves of the URL- http://digg.com, then the URL of delicious will be http://delicious.com/url/926a9b7a561a3f650ff41eef0c8ed45d The last part “926a9b7a561a3f650ff41eef0c8ed45d” is the md5 hash of the URL http://digg.com. The md5 is a one way hashing algorithm which converts a given string to a 32 character long string known as md5 digest. This hashed string can’t be reversed back into original string. The md5 function protects and ensures data integrity of Delicious Data Feeds. Delicious data feeds are read-only web feeds containing bookmark information and other information which can be used third party websites. These feeds are available into two different format: RSS and JSON. Among the various data feeds on the Delicious, let’s look at the details of the data feed which contains summary information of a URL. According to Delicious feed information page, these data feed for URL information can be retrieved via following call, http://feeds.delicious.com/v2/json/urlinfo/{url md5}It clearly specifies summary of URL can be retrieved in the json format only from Delicious. To get the summary about a URL, You can provide the actual URL in the url parameter of the above URL. Alternatively, you can provide md5() hash of the url in the hash parameter in the above URL. Now, let’s look at feed URLs which can be used to access the summary of the http://digg.com from Delicious: http://feeds.delicious.com/v2/json/urlinfo?callback=displayTotalSaves&url=http://digg.com OR From the above URLs, it is clear that md5 hash of the string "http://digg.com" is 926a9b7a561a3f650ff41eef0c8ed45d When JSON is used as the format of data returned form Delicious feed then you must specify the JavaScript callback function to handle the JSON data. Now, let look at the JSON data which is returned from any of the above URL of Delicious feed. displayTotalSaves([{"hash":"926a9b7a561a3f650ff41eef0c8ed45d","title":"digg",As you can see clearly, the above JSON data contains hash of URL, the URL itself, total no of saves in Delicious in total_posts variable. Along with them, different tags including number of times that tag is used by different users of Delicious for saving the URL http://digg.com. If the URL is not saved in Delicious then data returned from Delicious feed will be like this : displayTotalSaves([]) Now, having understod the information returned above, let’s see how to create Delicious widget step by step. Creating Delicious Tagometer WidgetOur Delicious Tagometer widget looks very similar to actual Delicious Tagometer widget but has different format and texts. In the Tagometer badge provided by delicious, there is no option for specifying a particular URL whose summary is to be displayed. It automatically displays the details of the URL of the web page containing the code. While in our custom widget, you can also specify the URL explicitly in the badge which is an optional parameter. For creating this widget, we will use JavaScript, CSS, XHTML and Delicious’s data feed in JSON format.
The above image is of the Delicious widget which we’re going to make and you can see clearly that the provided URL is not yet saved on Delicious. Now, let’s look at the Custom Delicious Tagometer which we can see for a URL saved on the delicious.
The above badge of delicious is displayed for the URL: http://yahoo.com. Writing Code for DeliciousFirst of all, let’s start looking at the JavaScript code for handling the parameters-url and title of the web page, when it is not provided. If these parameters are not defined explicitly then url and title of the web page using the widget is provided for saving the bookmark. if(typeof delicious_widget_url!='undefined') As can be seen from the above code, if the variable delicious_widget_url is not defined already then the URL of the current document encoded with encodeURIComponent() function is assigned to this variable. If delicious_widget_url variable is already defined then it is encoded with encodeURIComponent() function and assigned to the same variable. The typeof JavaScript operator can be used for checking the type of variable and also serves a very useful purpose of checking whether the variable or function is already defined or not. For example, the statement: typeof xyz==’function’ returns true if the function xyz is already defined. Similarly, if the variable delicious_widget_title is not defined then document.title, which contains title of current web page, is encoded with encodeURIComponent() function and assigned to the delicious_widget_title variable . Next, a variable del_rand_number is defined for handling multiple instances of the widget. var del_rand_number=Math.floor(Math.random()*1000); The Math.random() function returns random values between 0 to 1 and when multiplied by 1000 results in a random number between 1 to 1000. Since this random number is a floating point number hence floor() function of JavaScript Math Object is used for converting it to the greatest whole number which is less than or equal to generated random number. The Math object of JavaScript has three different functions which can be used to convert a floating point number to whole number: After that, now using above random number let’s define a variable which holds the “id” of the element in widget for displaying total no. of saves and tags. var del_saved_id="delicious-saved-"+del_rand_number; Now, let’s look at the initWidget() function for initializing the widget. function initWidget() At the start of the above code, the function writeWidgetTexts(); is called for writing the content of widget. The functions createElement() and setAttribute() are DOM manipulation function for creating a element and adding attribute to the element respectively. So, the next four statements of the above function create link element and sets various attributes for attaching CSS document to the page using widget. Once link element is created and various attribute is assigned to it the next step is to append it to the document using widget, which is done with appendChild() function in the next line of above function. getElementsByTagName() is a DOM function for accessing the document by the name of tag. Unlike getElementById() which access single element in the document, getElementsByTagName() can access multiple elements of DOM with the help of index. For example, document.getElementsByTagName("div")[0] or document.getElementsByTagName("div").item(0) refers to the first division element of the document Similarly, script element is created for getting JSON feed from Delicious. The call back JavaScript function displayTotalSaves() is used for handling JSON data returned from Delicious. AJAX and PHP: Building Responsive Web Applications
Now, first let’s look at the writeWidgetTexts() function for writing the content of Delicious Widget. function writeWidgetTexts() As you can see, the outer container of the widget is wrapped with division with class delicious-tagometer-widget. After that there is division with id stored in del_saved_id variable. Remember that this variable is generated with the help of random number. In the next statement, anchor tag id with class delicious-save-link is placed for displaying hyperlink to be stored in the variable delicious_widget_url for saving this url on Delicious. Finally, document.write() statement is used to write the content of widget which was stored in delicious_widget_text variable. Now, let's look at the callback function for handling JSON data returned from Delicious. function displayTotalSaves(data) In the above function, first it is checked whether data returned from Delicious feed is empty or not. You can review back to previous section for the detail of JSON data returned form Delicious. If JSON data is empty then the 0 is assigned to the variable delicious_total_saves otherwise this variable is assigned the value of variable total_posts of JSON data which can be accessed via variable data[0].total_posts. If delicious_total_saves variable contains other value than 0, which means that URL is saved in delicious, then we’ll display the number of saves to the widget element with id stored in the variable del_saved_id. Once number of saved is added, we now add the string " After that, top tags are added to widget which is available in top_tags in JSON data using for …in loop. The for..in loop is very useful loop for accessing object’s property. The basic syntax of this loop is Finally, the initWidget(); function is called to initialize the widget. After looking at the JavaScript code, now let’s look at the CSS. .delicious-tagometer-widget Now, let’s look at some of the important properties defined in the above styles. As you can see in the delicious-tagometer-widget class, the width of Tagometer is defined as 180px. In the delicious-save-link class, the delicious.small.gif image is placed at the background of the element with the x-coordinate being 10px and the y-coordinate being 3px. The background image repetition in element is set to no-repeat. In the delicious-save-number class, line-height property is set to 20px which sets the distance between upper and lower line of element. Putting the code all togetherSince we’ve dissected the code in sections, let’s compile them into single files. Create delicious_widget.js file and add the following code in it. if(typeof delicious_widget_url!='undefined') After that, create delicious.css file and add the following code in it. .delicious-tagometer-widget Using Delicious Tagometer WidgetTo use this Delicious Tagometer widget we’ve to follow these steps:
<script src="http://yourhost.com/delicious/delicious_widget.js"></script> OR <script type="text/javascript" language="javascript"> SummaryIn this article, we have seen how we can use data feed of Delicious to create the Delicious Tagometer badge. We’ve used JSON returned from data feed of delicious.com to create this widget which is pretty much similar to original delicious Tagometer widget. If you have read this article you may be interested to view :
Object-Oriented JavaScript
About the AuthorRoshan Bhattarai is an experienced web developer. His core proficiency lies in JAVASCRIPT, CSS, XHTML, PHP and MySQL. Having done his masters in information technology, he has many active websites to his credit. He has worked in various companies as a Technical Lead before moving on as a freelancer. He primarily resolves problems faced by programmers, liaise with the clients to have a better understanding of the projects' requirement and does research on new technologies relating to the project. He also excels in analysis and design of the database and work flow of a web project. He can be contacted on his blog http://roshanbh.com.np. Books from Packt
|
TOP TITLES ![]()
This is a 5 part mini series by Roshan Bhattarai, covering basics of Widget, development of Wiki seek Widget, Pop-up Image Widget, RSS Web Widget, and Delicious Tagometer Widget. The web is becoming more flexible and dynamic from day to day. The service and functionality provided by a particular website is not limited to itself. We can extend it to other websites by placing a few lines of code in their web pages called Widget. In this article we will explore the technologies that go behind making a Widget and understand its working. See More |
| ||||||||