Part 5: Statusbar and timeouts
The statusbar
Your JavaScript programs can write to the statusbar - this is the bar at the bottom of your browser window. All you have to do is to assign a string to window.status. The following example shows you two buttons which can be used to write to the statusbar and to erase the text again.
The script looks like this:
<html> <head> <script language="JavaScript"> <!-- hide function statbar(txt) { window.status = txt; } // --> </script> </head> <body> <form> <input type="button" name="look" value="Write!" onClick="statbar('Hi! This is the statusbar!');"> <input type="button" name="erase" value="Erase!" onClick="statbar('');"> </form> </body> </html>We create a form with two buttons. Both buttons call the function statbar(). You can see that the function call created by the Write! button looks like this:
statbar('Hi! This is the statusbar!');Inside the brackets we specify the string 'Hi! This is the statusbar!' This means this string is passed along to the function statbar(). You can see that we've defined the function statbar() like this:
function statbar(txt) { window.status = txt; }What is new is that we use txt inside the brackets of the function name. This means the string we passed along to the function is stored in the variable txt.
Displaying text on the statusbar can easily be used in combination with links. Instead of showing the URL of the link you can explain in words what the next page is about. This link demonstrates this - just move your mousepointer over the link. The code for this example looks like this:
<a href="dontclck.htm" onMouseOver="window.status='Don\'t click me!'; return true;" onMouseOut="window.status='';">link</a>Here we are using onMouseOver and onMouseOut in order to detect when the mousepointer moves across the link.
With the help of timeouts (or timer) you can let the computer execute some code after a certain period of time. I have created a button - if you press this button a window will pop-up after 3 seconds:
The script looks like this:
<script language="JavaScript"> <!-- hide function timer() { setTimeout("alert('Time is up!')", 3000); } // --> </script> ... <form> <input type="button" value="Timer" onClick="timer()"> </form>setTimeout() is a method of the window-object. It sets a timeout - I think you might have guessed that. The first argument is the JavaScript code which shall be executed after a certain time. In our case this argument is "alert('Time is up!')". Please note that the JavaScript code has to be inside quotes.
Now that you know how to write to the statusbar and how timeouts work we
will have a look at scrollers. You might already know the moving
text-strings in the statusbar. They can be seen all over the Internet.
We will see how to program a basic scroller. Besides that we will think
of possible improvements of the scroller.
Scrollers are quite easy to implement. Just let us think about how we
could realize a moving text in the statusbar. We have to write a text to
the statusbar. After a short period of time we have to write the same text
to the statusbar - but we have to move it a little bit to the left side.
If we repeat this over and over again the user gets the impression of
a moving text.
We have to think about how we can determine which part of the text should
be displayed as the whole text is normally longer than the statusbar.
This button will open up a new window and display a scroller:
Here is the source code - I have added some comments:
<html> <head> <script language="JavaScript"> <!-- hide // define the text of the scroller var scrtxt = "This is JavaScript! " + "This is JavaScript! " + "This is JavaScript!"; var length = scrtxt.length; var width = 100; var pos = -(width + 2); function scroll() { // display the text at the right position and set a timeout // move the position one step further pos++; // calculate the text which shall be displayed var scroller = ""; if (pos == length) { pos = -(width + 2); } // if the text hasn't reached the left side yet we have to // add some spaces - otherwise we have to cut of the first // part of the text (which moved already across the left border if (pos < 0) { for (var i = 1; i <= Math.abs(pos); i++) { scroller = scroller + " ";} scroller = scroller + scrtxt.substring(0, width - i + 1); } else { scroller = scroller + scrtxt.substring(pos, width + pos); } // assign the text to the statusbar window.status = scroller; // call this function again after 100 milliseconds setTimeout("scroll()", 100); } // --> </script> </head> <body onLoad="scroll()"> Your HTML-page goes here. </body> </html>The main part of the scroll() function is needed for calculating which part of the text is being displayed. I am not explaining the code in detail - you just have to understand how this scroller works in general.