Categories: ProgrammingTutorials

Development of Ajax Web Widget

10 min read

We’ve seen this kind of thing done by the popular bookmark sharing website, digg.com. We don’t need to visit digg.com to digg the URL. We can do it using small object called Widget, provided by digg.com. This is just one simple example of providing remote functionality; you can find lots of widgets providing different functionality across various websites.

What is a web widget?

A web widget is a small piece of code provided by a third party website, which can be installed and executed in any kind of web page which is created using HTML (or XHTML). The common technologies used in web widgets are JavaScript, Ajax and Adobe flash but in some places other technologies such as HTML combined with CSS, and Java Applets are also used. A web widget can be used for:

  • Serving useful information from a website.
  • Providing some functionality (like: voting, polling).
  • Endorsing the products and services of a website.

Web widgets are also known as badges, modules, and snippets. And, these are commonly used by bloggers, social network users and advertisers.

Popular Web Widgets

You can find many examples of the web widgets on the World Wide Web. These include:

  • Google Adsense – It is probably the most popular web widget you can see on the World Wide Web. All you’ve to do, is get a few lines of code from Google and place it in your website. Different kinds of contextual advertisements get displayed on your website according to the content displayed on your website.
  • Digg this widget – If You want to display news or articles then you’re most probably going to do so with this widget. Provided by the social book marking website digg.com, it is one of the popular bookmark sharing websites. Your website will lots of visitors if your article gets more and more digs, and gets featured on the first page.
  • Del.icio.us widget – You might have seen the widget previously, or text showing “Save to del.icio.us”, “Add to del.icio.us” and “saved by XXX users”. This is another popular social bookmarking website.
  • Snap preview –Provided by snap.com, this widget shows the thumbnail preview of a web page.
  • Survey widget from Polldaddy.com – If you need to run some kind of poll then you can use this widget to create and manage the poll on your website.
  • Google Maps – A rather well known widget, which adds different map functionality in Google maps. you must get an api key from Google to use this widget.

Benefits of Providing Web Widgets

I came to know about dzone.com, a social bookmarking website for web and software developers, from a widget I found in a blog. Let’s look at the benefits of web widgets:

  • A widget can be an excellent publicity tool. Most of the people who come to know about digg.com, del.icio.us or reddit.com do so from their web widgets in various news portal and blogs.
  • Providing web services- This is useful since the user doesn’t have to visit that particular website to get its services. For example, a visitor to a website which has a Polldaddy web widgets can participate on a survey without visiting to polldaddy.com.
  • Web widgets can be one of the ways of bringing income for your website, by utilizing advertising widgets such as Google adsense (discussed above).
  • Widgets can generate more traffic to your website. For example, you can make a widget that evaluates the price of a product on a website. Widgets across multiple websites means visitors from multiple sources, hence more traffic to you website.

How do Web Widgets work

As can be seen in the above figure, the web widget is used by a widget client, which is a web page. The web pages just use a chunk of code provided by the functionality providing website. Furthermore, all the services and functionality are provided by the code residing on the remote server which the widget client doesn’t have to care about. In a web widget system, there is optional system called Widget Management System which can manage the output of the widget.

Widget Management System

A Widget Management System is used for controlling the various operations of the widget, including things like height or width or color. Most widget providing websites allows you to customize the way widget looks in blogs or website. For example, you can customize the size and color of Google adsense by logging into the Google adsense account system.

Some widget Management Systems also allow you to manage the content of the widget as well. In the survey widget from vizu.com, you can manage parameters like the topic of poll and number of answers of the poll.

Concept of Technologies Used

To make the most out of Ajax web widgets, let’s get familiar with some technologies.

HTML and XHTML

Hypertext Markup language(HTML) is one of the most known markup language for constructing web pages. As the name implies, we use it to markup the text document so that web browsers know how to display them.

XHTML on the other hand, is the way to standardize the HTML by making HTML a dialect of XML. XHTML comes in different flavors like transitional, strict or frameset, with each flavor offering either different capabilities or different degrees of conformance to the XML standard.

Difference between HTML and XHTML

The single biggest difference between HTML and XHTML is that XHML must be well-formed according to XML conventions. Because an XHTML document is essentially XML in nature, simply following the traditional HTML practices is not enough. An XHML document must be well formed. For example, let’s take the examples of “img” and “input” elements of HTML.

<img src="logo.gif" alt="My Site" border="0" >
<input type="text" name="only_html" id="user_name" value="Not XHTML" >

Both of the above statements are perfect HTML statements but they are not well graded constructions in XHTML as they are not well formed according to compliance of XML (those tags are not enclosed properly). To figure out the difference, first look at the above tags in the XHML valid syntax.

<img src="logo.gif" alt="My Site" border="0" />
<input type="text" name="only_html" id="user_name" value="Not XHTML" />

Did you find the difference? The latter tags are well formed and the previous one is not. If you look closely, you can find that the HTML tags in the second example are closed like XML. The point is you should have well formed document in order to allow JavaScript to access the DOM.

You can also add comments in the HTML document. Any information beginning with the character string <!– and ending with the string –> will be ignored:

<!-- This is a comment within HTML-->

You can add the comment in the same fashion in an XML document as well.

Concept of IFRAME

IFrame (Inline Frame ) is a useful HTML element for creating web widgets. Using IFrame, we can embed the HTML document from the remote server into our document. This feature is most commonly used in the web widgets.

We can define the width and height of IFrame using the height and width property of the IFrame element. IFrame can be defined in any part of the web pages. IFrame behaves like an inline object and the user can scroll through the Iframe to view the content which is out of view. We can also remove the scroll bar from the IFrame by setting scrolling property to no.

The embedded document can be reloaded asynchronously using IFrame and can be an alternative option to an XMLHttpRequest object. For this reason, IFrames are widely used by Ajax applications.

You can use the IFrame in the HTML document in the following way:

<html>
<body>
<iframe src="http://wwww.example.com" width="200">
If your browser doesn’t support IFrame, this text will be
displayed in the browser
</iframe>
</body>
</html>

CSS(Cascading Style Sheet)

A Cascading Style Sheet is a style sheet language which is used to define the presentation of document written in HTML or XHTML pages. CSS is mainly used for defining the layout, colors and fonts of the elements of a web page along with other perspectives of the document. While the mark up language can be used to define the content of the web page, the primary goal of providing a CSS is to separate the document’s content from the document’s presentation. The Internet Media type “text/css” is reserved for CSS.

Using Cascading Style Sheet

CSS can be used in different ways in a web page. It can be written with the <style></style> tag of the web-page. It can be defined in the style attribute of the element. Also, CSS can be defined in a different page with extension .css that links with the web page to define the presentation.

What is the benefit of using CSS then? You can define the values for a set of attributes for all HTML elements (of a similar type), then change the presentation with one change in the CSS property

<style type="text/css">
all the css declaration goes here
</style>

But what about defining the CSS property for different web pages? The above method can be cumbersome and you would need to replicate the same code to different web pages. To overcome such a situation, you can define all the property in a single page let’s say call it style.css and link that CSS to the web pages.

<link href="style.css" type="text/css" rel="stylesheet" />

Furthermore, you can define the styles within the elements using the style property of the HTML elements, which is commonly known as inline styles.

<p style="text-align:justify">This is content inside paragraph</p>

As you’ve seen, we’ve quickly defined the text-align attribute to justify

Defining Style Sheet rules

A style sheet allows you to change the different attributes of the HTML elements by defining a styling rule for them. A rule has two components: selectors, which defines which HTML elements to apply the styling rule and declaration, which contains the styling rule for the selectors.

p { background-color:#999999 }

In the above css rule, p is the selector and background-color:#999999, is the declaration of the rule. The above style defines the rule for the paragraph element <p></p>.

.box { width:300px }

In the above rule, selectors is defined as a class and it can be used in any element with the class property of the element.

<p class="box">..content..</p> <div class="box">content</div>

Class can be used with any HTML element.

#container { width:1000px }

“#” is known as an id selector in CSS. The above rule applies to an element which has the id “container” such as below:

<div id="container">..content..</div>

We can also define a set of rules for multiple elements using comma (,) among the selectors:

h1, h2, h3 {font-size:12px; color:#FF0000 }

There are many more styles of defining selectors and CSS properties.

Packt

Share
Published by
Packt

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago