Building a Website that will Alert, Confirm and Prompt.

A Javascript Alert Box - Mark Alexander Bain
A Javascript Alert Box - Mark Alexander Bain
All websites consist of text and images which may lead the user to passively absorb some of the information. However, Javascript can improve that experience

Many websites simply display text and images. They're a one way process with no interaction between the web page and the web page user. However, that's not really natural for human beings – they are more used to two-way communications both at a conscious level (for example talking and listening) and a subconscious level (for example body language).

The question is, therefore, can a web page developer create a two-way dialogue with their users. The answer is – yes. With the introduction of just a few simple techniques a web page can obtain information from its users rather than just presenting it to them.

Simple Alerts and Confirmations

The simplest communication can be achieve by using the javascript alert function, for example within the onclick even of a button:

button onclick="Javascript: alert('You clicked me!');

However, even that is rather one sided. The first glimmering of an actual conversation starts with the use of the confirm function. Now the script can obtain some information from the user:

var response = confirm("Do you want to continue?");

This will just return a simple true or false condition. The next step is to obtain some text from the user.

Prompting with Javascript

It's possible to create a simple yes/no conversation going by using just the confirm function:

var legs = confirm("Do you have four legs"); var meow = confirm ("Do you meow?"); if ((legs)&&(meow)) alert ("You are a cat");

The prompt function enables the web page developer to obtain a written response from the user:

var animal = prompt ("What kind of animal are you", "Human");

In this example an editable text box will be displayed. The first parameter will appear as a title and the second will populate the text box. It will return either the contents of the box (if "OK" is clicked) or null (if "Cancel" is clicked).

Each of these gives makes the site much more interactive for the user. Rather than taking a passive role they become part of the experience. And, in fact, this may help the user to remember the content of the web site more effectively. Current research shows that text written in easy to read fonts (such as Serif) is less well remember than text written less readable fonts and may impair cognition.

It may well be that by giving the user more to do on the web page the developer may actually create a more informative site, not by providing extra content, but by providing extra activities.

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement
Advertisement
Advertisement