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.
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…