“ I hear and I forget. I see and I remember. I do and I understand.” -Confucius

IN THIS CHAPTER

Create a Hello World Widget

How to pass a parameter to the Widget

How Widget processes data

What could be a good data source for Widget

Introduction

A web widget in simple terms is a client side, browser based application, with a small functionality, and which displays data coming from different sources.

In this chapter, we will create a basic Hello World Widget using HTML, CSS, and JavaScript. This chapter gives you a good starting point to understand the development of a web widget. If you are a beginner, I would suggest you to do this chapter along with me, and get a good grasp of all the aspects of the web widget. In the next section, we will create a more advanced widget and you will learn all aspects of it, like customization,security and performance as in the real world scenario. This chapter first shows the different components of the Hello World widget, and then takes in a step by step process, to develop it.

We will use Microsoft’s Visual Web Developer Express (free) edition for developing the Widget, but you can use your favorite Integrated Development Environment (IDE).

Hello World Widget

Before diving into developing the widget, let’s see what it does. The Widget takes two parameters with values ‘Hello’ and ‘World’, one is passed from the test page where it is hosted and the other comes from the data which is hosted in the server, joins them and display it together along with the image as shown in Figure 5.1. When you click on the ‘Hello World’ text, it redirects to a website http://widgets-gadgets.com, which is also passed as a parameter from the page.

Figure 5.1

Widget Overview

The main functionality of the widget is to display data based on the data from the server and the parameter passed in the page. The most common data for a web widget is JSON (JavaScript Object Notation) file format. The reason being that, the JSON data can be used cross domain, so what that means is a JSON data can be residing in a different server and the web widget on a users page can include and use it using a function call. Essentially the JSON data becomes the part of the parameter of the function call.

For example, to include JSON data from the server, include a JavaScript file data.js, with a single function with JSON data as parameter as shown below:

The actual JSON data can be an array of any number of key value pairs which follows the JavaScript object notation. More information at JSON.org. The ParseData function will be in another JavaScript file, say myWidget.js file. And when the JavaScript file is loaded, the function is called and the JSON data is then parsed and displayed using Document.Write.

The important point here is that the data.js file can be dynamically created on the server side, here it is JSON data but it can also be dynamically created series of JavaScript document.write lines which renders HTML directly. Although having JSON data gives modularity to the approach.

Let’s now see the Hello World Widget architecture.

The architecture of a widget can be divided into three parts as shown in Figure 5.2.

Data: The data of the widget, coming from data.js

HTML: The page which hosts the widget and passes the parameter to the Widget

JavaScript: The main functionality of parsing and displaying data

Images and Style: Presentation of the Widget

In the section, we will create a widget which takes the JSON data coming from the server and parses and displays it in the Widget as Hello World. Open your favorite HTML editor and let’s get started.

Figure 5.2

Developing the Hello World Widget

The Hello World Widget will display ‘Hello World’ along with the image of the widget in the viewable area of the widget. Here are the parts of the widget as shown in Figure 5.3.

The core functionality (widget.js) of the widget which takes the data and displays it

JSON data file (data.js) which is hosted in the server

The presentation of the Widget (style.css)

Two images, widget and the background

Widget Page

Create a new HTML page and name it testpage.html and enter the code shown below. The widget code in the page contains few variables and JavaScript to include script to add a DIV element “myFirstWidget” and to include the bootstrap.js file. Don’t worry about them now, we will create and see what bootstrap does later.The variables can also be given a local scope by encapsulating all the lines inside a single function and calling that function.

Note that we are also not including the JavaScript file using HTML code but rather using the JavaScript code, this is to group the widget together as a single chunk of embeddable code. The HTML element myFirstWidget is the main element that is used to render the widget data as you will see later. The JavaScript file bootstrap.js contains the logic to load the widget functionality and the data. Let’s create bootstrap.js next.

Figure 5.3

Bootstrap

The bootstrap.js file is used to make the widget more modular, think of it as the light weight file which is loaded with the page but can load other files as needed. The widget might have multiple JavaScript files so a Bootstrap file is the single file which can contain all the code for initialization.

In our bootloader, we have more JavaScript code to load the data and the functionality of the widget. In the later chapter, we will see how we can use bootloader to delay loading of the widget which frees the page from loading. Note that widget.js is loaded first and data.js is rendered later, the reason is the data.js calls the function which is declared in the widget.js.

In HelloWorld Widget, we need three things from the bootloader:

Loading the presentation style sheet, style.css

Loading the JSON data, data.js

Loading the logic of the widget, Widget.js, this takes the data and parses it

Presentation with HTML, CSS and Images

The first thing the bootstrap code loads is the style sheet because it should be loaded before we use it in the widget code. The presentation of the widget consists of both style sheet and the images which are used to display widget data. For our widget, let’s create a style.css and image(s) to be used for widget and background (see figure 5-4).

Create a new file Style.css and add it to your project. A separate stylesheet file allows you to change the look and feel of the widget without touching the widget functionality. Enter the following code.

Note here that we have a single style to be used on the widget DIV element later. The images are included in the source, but you can use a photo editing software to create them.

As mentioned earlier, the data.js is basically a function call with JSON data as parameter. We will create the function WidgetCallback in the Widget.js file. Note the key value pair for each parameter, each of them can be accessed using the dot notation in the called function. Next we will create the Widget.js.

Core Functionality with JavaScript

Create a new JavaScript file and name it widget.js.

The JavaScript file widget.js will have a single function WidgetCallBack. The function will read the JSON data and will generate HTML code based on the values in the data passed.

Here is the code of the file widget.js.

Note that both the color palette displays a limited set of colors, which is called web safe colors (more on this in the next section) and includes gray colors. A color pallette has become a standard for widget color customization, so let's see what is needed to create a color palette.

Note that the function WidgetCallBack is called as soon as data.js is loaded completely.

As you see, the code creates the HTML code for Hello World. It takes values from the parameter passed from the test page as well as data from the JSON object.

wWidth used for defining the width of the Widget

wHeight used for height of the Widget

wWidgetHelloWorld.image_url used for background image of the Widget

wFColor used for the foreground color of the Widget Text

wTitle and wHelloWorld.text together to display “Hello” and “ World”

Note once the HTML is rendered, the DIV element myFirstWidget’s innerHTML is updated.

That’s it! Now we have created the files required. Just open the test page in the browser and you should be able to see the Hello World Widget in action. Right now, we have all the files locally so what I want you to do is make a small change in the bootstrap.js file, and run the test page again.

The point here is the widget is independent of where the data is located, and so can be hosted in any web page in any domain.

Widget Data Display Complete Process

In Hello World Widget, JSON data is the link between the server side data and the widget. The JSON data is meant to be created dynamically by server side code and is consumed by the widget residing on the web page. The process of displaying Hello World starts from the parameter “Hello” which along with other parameters are passed to the widget. The Widget is then loaded along with the Data, from where comes the text “World” the widget functionality joins the two text and displays it in the Widget Element. Figure 5.5 shows the architecture of the Widget for Hello World Widget.

Here are the steps for data display of Hello World Widget:

Test page loads the widget loaded with parameters

Widget calls the data.js which has the dynamic content

The style sheets are loaded for the widget presentation

The widget functionality includes parsing the dynamic content

The widget functionality uses parameters passed to render the final HTML code

Final Widget display HTML is passed back to the web page

Figure 5.5

Summary

In this short chapter, we learnt how to create a simple ‘Hello World’ Widget. We saw how a widget can display data from a remote server:

Widget can be divided into three parts

Core functionality in the form of Script

Data from the remote host

Presentation in the form of Style sheet and images

Widget interaction includes

Parameters passed from the page where it is hosted

Dynamic data which comes from the server

Script code which renders the Widget code based on the parameters and the data

Comments and Discussions

I can see the last post on this thread was a while ago so I'm hoping to still get an answer. I'm thinking of getting your book, however I'm trying to avoid using any frameworks like Bootstrap but noted in some of the comments that Bootstrap is used with some of the examples in your book. Does the book explain creating widgets without using Bootstrap?

Also, the example website url does not work. Are you going to fix this?

One of the commenters is right. The code is unnecessarily convoluted. The web page loads bootstrap.js, which then loads WidgetCreate.js via a script tag, then loads data.js by appending a generated script block. Perhaps a simple "hello world" widget example would have helped, which the author could then have expanded on to create this more scaleable example.

While I appreciate the subject, the way this chapter is written makes me concerned about the content of the book.

1) This line will not work in some browsers: document.write('<scr'+'ipt type="text/JavaScript" src="bootstrap.js" />');it should be: document.write('<scr'+'ipt type="text/JavaScript" src="bootstrap.js"></'+'script>');

2) Inconsistent file naming. For instance, you use widget.js but show WidgetCreate.js. Minor, but doesn't build confidence.

3) You load widget.js, but then call WidgetCallback without verifying that widget.js has finished loading. This will not work in some browsers since using document.write loads without blocking, so the callback can occur before the function has loaded.

I am still yet to get your book since it is an international version and has to be ordered from abroad!! I was trying a few things from my end with this article of yours and wanted to know :

1) Is the WidgetCreate.js file going to be residing on my Remote server location ? 2) The example you've given seems to be taking data (json call) from the same server as it was called from ? The image that is displayed comes from the image URL that you're providing, which again seems to be residing in the server, since I noticed the Json data as : "image_url": "images/widget.gif", - which normally should have been http://www.widget-gadget.com/images/widget.gif. Do correct me if I'm wrong.

The code is unnecessarily convoluted. The web page loads bootstrap.js, which then loads WidgetCreate.js via a script tag, then loads data.js by appending a generated script block. Why two different ways of achieving the same result, when two simple script tags would do? Why not do away with bootstrap.js entirely and put those two script tags directly in the web page?

Thanks for your message, a simple hello world widget could have been done using plain JS code in the HTML page but if you are creating a production level web widget you need to keep performance, and maintainability in mind. In a real scenario, a web widget have data coming from a remote server and processing is done on the data:

1. We use bootstrap.js approach, so that the widget does not suspend the page in which it is hosted, this is further explained in the "Making High Performance Widget" chapter

2. The data coming from remote server is parsed, extracted and displayed this needs to be done in a modular way to enable maintainability and scalability thats why you see WidgetCreate.js.