Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 04:33, 10 February 2012 by hamishwillee (Talk | contribs)

Progress Bar JavaScript component for Web Runtime

From Wiki
Jump to: navigation, search

This article explains how to create and use a Progress Bar component in Web Runtime widgets. A Progress Bar could be useful during long-running operations, to give a visual feedback to the user about the operation status. Two types of Progress Bars will be handled by this component: with definite and indefinite ranges.

Article Metadata
Code ExampleArticle
Created: jappit (08 Jun 2009)
Last edited: hamishwillee (10 Feb 2012)

Wrt progressbar widget.png

The "Wait and Progress indicator design pattern" is detailed described by this Nokia Developer Wiki article: Mobile Design Pattern: Progress and Wait Indicator.

For more information about progress bars' usage, and guidelines about actions and visual feedback to the user, check out the Actions and feedback section of the Design and User Experience Library:


ProgressBar component: how to use it

To use the Progress Bar component presented in this article, follow these steps:

<script language="javascript" type="text/javascript" src="ProgressBar.js"></script>
  • Include in your project 2 images, of the same size, for the empty and full progress bar, respectively. Sample images, used in this article, are the following:

Wrt progressbar empty.png Wrt progressbar full.png

  • Define a parent HTML node for the component in your Widget HTML code:
<div id="loading_bar"></div>
  • Finally, create a ProgressBar instance, passing these arguments to its constructor:
    • the empty bar image path
    • the full bar image path
    • the progress bar width
    • the progress bar height

And then append the newly created instance to the HTML parent node defined above:

progressBar = new ProgressBar('images/progress_empty.png', 'images/progress_full.png', 200, 28);

ProgressBar component implementation

This part of the articles shows how the ProgressBar JavaScript component is actually implemented.

ProgressBar constuctor

The first part to implement is the ProgressBar constructor, whose arguments were already shown in the usage part.

function ProgressBar(baseImagePath, fullImagePath, barWidth, barHeight)
/* DOM elements */
this.fullImageElement = null;
this.domElement = null;
/* Progress Bar size */
this.barWidth = barWidth;
this.barHeight = barHeight;
/* loading progress */
this.progressValue = 0;
this.init(baseImagePath, fullImagePath, barWidth, barHeight);

The costructor initializes the size-related properties of the ProgressBar, sets the initial progress status to zero, and then calls the init() method that will builds up the component DOM structure.

Building and using component's DOM structure

The whole component DOM structure will be built by the init() instance method, that is implemented as follows:

ProgressBar.prototype.init = function(baseImagePath, fullImagePath, barWidth, barHeight)
var container = document.createElement('div'); = 'relative';
this.domElement = container;
var baseImage = document.createElement('img');
baseImage.src = baseImagePath; = 'absolute';
var fullImageContainer = document.createElement('div'); = 'absolute'; = 'hidden'; = barHeight + 'px'; = 0;
this.fullImageElement = fullImageContainer;
var fullImage = document.createElement('img');
fullImage.src = fullImagePath; = 'absolute';

The component is structured as follows:

  • an outer DIV element, that contains all the component
  • the empty bar image, completely shown
  • an inner DIV, containing the full bar image, that initially has width equal to zero. Having the 'overflow' CSS property set to 'hidden', the full bar image will be initially hidden, and will be partially or totally shown proportionally to the progress status.

To append the component main DOM element to a parent element defined in a widget, the following appendTo() method is defined:

ProgressBar.prototype.appendTo = function(parentElement)

Updating the progress status

The following setLoadingProgress() method manages the updates of the progress bar. It accepts as argument the new progress value, that can be any numeric value between 0 and 100, and then updates both the progressValue property, and the UI status of the component.

ProgressBar.prototype.setLoadingProgress = function(value)
if(value < 0)
value = 0;
else if(value > 100)
value = 100;
this.progressValue = value; = (this.barWidth * value / 100) + 'px';

To update the component UI status, the width of the fullImageElement (the DOM element containing the full bar image) is changed proportionally to the new progress value.

Indefinite progress bars

The setLoadingProgress() is useful if you want to show the exact progress status to the user. But, there are situations where the exact progress status is not known, and the only thing available information is that the long running operation is still running. In these cases, it is useful to show a progress bar that is always animated, so restarting when it reaches the full status.

First, let's define two ProgressBar properties that will be used to handle the indefinite progress bars:

function ProgressBar(baseImagePath, fullImagePath, barWidth, barHeight)
/* progressbar type */
this.indefiniteProgress = false;
this.indefiniteInterval = null;

Then, the functionality is implemented through the following setIndefinite() method:

ProgressBar.prototype.setIndefinite = function(isIndefinite)
if(this.indefiniteProgress != isIndefinite)
this.indefiniteProgress = isIndefinite;
this.indefiniteInterval = null;
var self = this;
this.indefiniteInterval = setInterval(
var nextValue = self.progressValue + 2;
if(nextValue > 100)
nextValue -= 100;

If the isIndefinite argument is true, then the progress bar is treated as indefinite, and an always running animation is started. Otherwise, if it is false, the animation, if running, is stopped.


The following files, used in this article, are available for download:

118 page views in the last 30 days.