Web widgets are widely used across the Internet but still lacks good documentation. From online advertisement to videos to blogs, widgets are highly used. Some of the popular widgets being Google Adsense, Youtube, MyBlogLog widgets and Twitter badges.
Note: This page will be slow to load because of many widget examples.
To formally define : The web widget is a portable chunk of code that can be installed and executed within any separate HTML-based web page by an end user without requiring additional compilation. A widget adds some content to that page that is (mostly) not static. Generally widgets are third party originated, though they can be home made. Widgets are also known as modules, snippets, and plug-ins.
This article is my journey of understanding and making a widget myself. I have tried to make things look simple and insightful by taking a lot of examples.
Before you start
Clearly identify the information which will be distributed by your widget or the information you would like your client to embed in their webpage. Then find the most suitable mechanism to send this information. Mechanism can be one of the following :
Flash (need not be static i.e. it can take parameters) as a widget.
Simple HTML page
JavaScript
JSON API
Flash widget
This is a highly used method for making widgets. Most of the flashy irritating advertisements seen on many websites are flash widgets. A good usage being youtube videos and twitter badge type-1. Flash widget code includes an "embed" or "object" HTML tag with the source to flash file. Extra parameters may be passed to flash file to customize flash content. And remember, ActionScript is always there for more magic :-)
HTML widget
Instead of flash embed an HTML page or HTML code at client's webpage using iframe. This can be as simple as:
Mostly used when content is static for all users (like brand advertisements). Flash and HTML widgets are the most simplest to develop but can be of limited use in many cases.
JavaScript widget
JavaScript is the most powerful tool to make widgets. Top examples being, Google Adsense and MyBlogLog recent visitors widget. There are two ways to develop widgets via scripting:
Passive widget : The widget code is a script tag which links to external JavaScript file and this file has the code to directly print the HTML markup on client's webpage. AideRSS provides a passive widget. For example::
// script-raw-dump.js contains the following codedocument.write("<ul>");document.write("<li>Hello world</li>");document.write("<li>Hello world2</li>");document.write("</ul>");document.write("Powered by <img src='quarkruby' alt='quarkruby'/>");
Output seen on webpage will be:
Active widget : As the name says, they do work/processing/calculations before just printing HTML code. The widget code contains a script tag to download one or more external JavaScript files, which does work (getting client information, passing information around, etc.) and then HTML is dumped at client side. Google adsense, Mybloglog's widget all fall in this category.
Active widgets can be very powerful if, each client is given a unique identifier by the widget source for user identification. This allows widget providing site to send/collect user-related data. Active widget's can be made in two ways :
Using simple JavaScript Sample widget code:
12345
<scripttype="text/javascript">unique_key_each_host = "quarkruby_123";// some other variables ..</script><scriptsrc="http://www.quarkruby.com/assets/static-javascript-variable.js"></script>
What the code does:
- Get the JavaScript as mentioned in last line above
- This JavaScript file when executed, will read the values of above defined variables + possibly collect other information A sample of static-javascript-variable.js:
This creates an iframe element at client's webpage and set the source to an external url (defined in static-javascript-variable.js file) passing above calculated parameters. The output fetched is an HTML page which is inserted iframe created in last line of code
JSON widget
For using JSON API's or JSON data. Twitter badge type 2 is a good example of widget using JSON calls. Sample Widget code:
- The first JavaScript contains the callback function definition (possibly other functions also). These will process the JSON data, generate HTML code and insert into DOM at "#quarkruby_widget" element using innerHTML method or DOM manipulation by creating/adding/appending elements.
- This embeds a flash file passing it color of widget, user_id as variables. So, after flash file is loaded, it makes request for user specific data (mentioned in user_id variable) and loads it later.
Fetches the JavaScript file mentioned in last line.
This file then uses the defined parameters and gathers value for hidden parameters (so that resulting ads are more local and content specific).
Finally creates an iframe element at client's webpage and set the source to the external url passing above calculated parameters. The output is an HTML page which is inserted into this iframe.
The second JavaScript link is the JSON call to their API, which fetches the JSON data and calls back "twitterCallback2" function which is defined in blogger.js file. Then blogger.js downloads the JavaScript code which processes the JSON data and inserts the final output into "twitter_div" element.
The JavaScript file mentioned in script tag is fetched which contains the JSON data + link to more JavaScript files. These additional JavaScript files are fetched and then JSON data is processed and using DOM manipulation output is inserted into webpage.
- Fetch this file, which generates HTML code + CSS data for the final output
- Fetch one more JavaScript file linked in the above mentioned file which is used for tracking the users.
Output :
Key issues
Slowing down loading of client's site.:
What are you talking? Yes, widgets can bring down client's site who is embedding widget code in their page because:
script tag stops page rendering until code inside it is executed and rendered.
Script tag blocks parallel downloads :(
If the widget source goes down or is slow, the client's webpage won't load unless the widget code is executed and rendered. To avoid this problem consider following workarounds :
Use "defer" attribute with script (firefox doesn't support this though :-( ). This tells the browser to continue rendering and do not block the other downloads. Moreover, this can only be used if there is no "document.write" statement in your script. See more details here
Put timeout on script attachment (source: WEDJE)
Here, external scripts in widget code are not downloaded during page load, but attached after sometime. So, this does not affects loading/rendering of site. Hence, after sometime, the external scripts are downloaded, attached/executed thereafter. In example mentioned below, Twitter type-2 badge is modified to download the external scripts after 1 sec. So, if you embed this code in some html page, you will see the output after delay of 1sec Example: modifying the twitter widget code :
Avoid conflict's with client's JavaScript: Widget code and external loaded Javascript files should contain minimum global variables, so as to prevent any chances of conflict with client's JavaScript which is embedding the widget. If your widget does not take any parameters from client, one should try to use anonymous JavaScript classes.
Let client apply its own CSS to widget: This helps in customizing the style of widget as per the design/looks of client's webpage. Google allows this even via iframes by using global variables, Amazon allows this while making widget, Mybloglog and twitter doesn't use iframes so your CSS directly applies to the widget's HTML.
Minimize number of request: This is to load your widget faster which applies to any HTML page
Do you want to prevent content editing?: If you do not want to allow content editing of the widget, then use iframes since they cannot be manipulated by JavaScript outside the iframe element.
Using API If you are using an API, then callback method must be provided by that API. This is just to keep in mind if you want to use some external API and display the content dynamically.
Excellent resource. I think if anything widgets have gained popularity over the last 6 months. Most websites want to give the user a better experience, and this is what widgets do.
All very useful information for site developers especially novices like my self I plan to use a widget to develop my site as per your directions. will let you know feedback once complete. Thanks for the tips.
tanzanite
Hi, very cool tutorial. I think small widgets will best work in an iframe if not dependent of any client stuff. And I could drop a lot of JavaScript code this way and use HTML directly, couldn’t I?
Thank you for the article. Amazon widgets tend to be on top layer of everything. e.g. It is above any html pull down menus and pop-ups. Is there a way to fix this?
Thanks for the article, good start for writing a widget.
One thing to ask about: Is it possible to use, lets say, auto complete for user input for example (Ajaxified results) when writing a widget?
I have been looking for something like this, but for Twitter badges/feeds, been told to use the same method Google Analytics use to ensure the Twitter badge will work on a secure page too? Any advice or help? Twitters site is down at time of writing this, which has slowed down our site too. We use the html widget from Twitter, but we cannot put the <js> at the bottom of the page (footer), as advised before the close </body> tag, as this same footer is used across the entire site, including http and https pages.
If the standard Twitter badge is used on a https page, it throws up SSL alerts in IE.
Thanks for the article, good start for writing a widget. One thing to ask about: Is it possible to use, lets say, auto complete for user input for example (Ajaxified results) when writing a widget?
Widgets made clear! Thank you very much. Our site is seriously lacking in most of the tenets you have outlined here, perhaps as a result of developer ignorance. This information was invaluable.
I have a dumb question if I may. About the Adsense code,
You said
This file then uses the defined parameters and gathers value for hidden parameters (so that resulting ads are more local and content specific).
But where are these hidden variables defined. And since if you change the content of the page, the adsense changes the ads based on the new content, how does the java script changes the ads based on the content on the page.
ckd
January 21, 2010 @ 08:39 AM
This was helpful. Thanks! Got a question though.. so how can one actually write a new widget? (not creating using sites such as widgetbox.com, etc.)
About
QuarkRuby is a blog of Nakul Aggarwal & Ritesh Arora who are currently living a Ruby life.
Feel free to buzz them at quarkruby@gmail.com or please leave a comment.
Pretty Cool. looking forward to more…
Nice advice. did you already created some widgets yourself? would be nice to show them.
best regards
@forex
yes we did, you can find our widget at http://www.quarkrank.com/widget.
We will try to get with explanation on how we developed it.
Thanks for the information, I found it really useful!!. However, I don’t know anything about JSON, and I got lost in that part…
Hi , The code given is really help me lot. Thanks a lot for the suggestion and the code.
hm, very interesting, but would be helpful to see some examples.
Reference: Very helpful, thanks!!
Excellent resource. I think if anything widgets have gained popularity over the last 6 months. Most websites want to give the user a better experience, and this is what widgets do.
thank you, it is very nice
All very useful information for site developers especially novices like my self I plan to use a widget to develop my site as per your directions. will let you know feedback once complete. Thanks for the tips. tanzanite
Hi, very cool tutorial. I think small widgets will best work in an iframe if not dependent of any client stuff. And I could drop a lot of JavaScript code this way and use HTML directly, couldn’t I?
Thanks for article good widgets
Thank you for the article. Amazon widgets tend to be on top layer of everything. e.g. It is above any html pull down menus and pop-ups. Is there a way to fix this?
nice article, thanks guys
Thanks for the article, good start for writing a widget. One thing to ask about: Is it possible to use, lets say, auto complete for user input for example (Ajaxified results) when writing a widget?
Thanks again ;)
Thank you for useful information, now more enjoyable web design
Thank you very much, nice work
Thanks a lot. It’s very enlightening.
Hi,
I have been looking for something like this, but for Twitter badges/feeds, been told to use the same method Google Analytics use to ensure the Twitter badge will work on a secure page too? Any advice or help? Twitters site is down at time of writing this, which has slowed down our site too. We use the html widget from Twitter, but we cannot put the <js> at the bottom of the page (footer), as advised before the close </body> tag, as this same footer is used across the entire site, including http and https pages.
If the standard Twitter badge is used on a https page, it throws up SSL alerts in IE.
Welcome feedback from others on this!
Thanks for the article, good start for writing a widget. One thing to ask about: Is it possible to use, lets say, auto complete for user input for example (Ajaxified results) when writing a widget?
Thanks again ;)
nice tutorial, helped me a lot. thanks
cool! Thank you for useful information!
Can anywone give a working example of widget written in asp (server side).
I want some info from my database at “myWebsite.com” to be displayed on the client’s website. I am using asp classic on my server.
Excellent resource on widget development.
Great help in giving me a start to include widgets in my website for my users.
I still prefer to create a Flash widget for my website, for which i found the information given here is incomplete.
Can anyone please explain how to create a flash widget. I don’t know much about flash. My source data file is in .asp
Widgets made clear! Thank you very much. Our site is seriously lacking in most of the tenets you have outlined here, perhaps as a result of developer ignorance. This information was invaluable.
I have a dumb question if I may. About the Adsense code, You said This file then uses the defined parameters and gathers value for hidden parameters (so that resulting ads are more local and content specific).
But where are these hidden variables defined. And since if you change the content of the page, the adsense changes the ads based on the new content, how does the java script changes the ads based on the content on the page.
This was helpful. Thanks! Got a question though.. so how can one actually write a new widget? (not creating using sites such as widgetbox.com, etc.)