Looking at Some Events
<font color="#ff0000" size=5>WebTV JS "Bug" - Reload Page (cmd-r)</font>

Events are things that happen. A page is loaded; a link is clicked; a cursor is moved to a particular location. These are all events. JS has what are called "event handlers". These are codes which cause something to occur when a particular event takes place. For example, consider the events below:

<a href="javascript:void(0)" onClick="alert('Message');">Click Me</a>


<a href="javascript:void(0)" onMouseOut="alert('Message');">Move Cursor Away</a>



<a href="javascript:void(0)" onMouseOver="alert('Message');">Move Cursor Over Me</a>

Click Me


Move Cursor Away


Start/Stop Here


Move Cursor Over Me


The events Click, MouseOver, and MouseOut preceded by "on", designate that when the event takes place do what follows, in this case generate a pop-up. Note that what follows is a JS statement ending with a semicolon. A series of JS statements could be done just as well. The event Load has been used previously in discussing background and text color and in making a clock. In these instances the event handler was onLoad.

Some particulars merit note. WebTV supports only a limited number of events that can be enabled by an event handler. Things like onMouseUp and onMouseDown, onKeyUp and onKeyDown are not supported, but onKeyPress is now. There may be, at times, a problem with link events when creating pop-ups such as "alert". There is nothing following the href, it may be just two quotes with no space(s) in between or there can be #1 between the quotes. These will work with WebTV, but sometimes the browser looks for a link to go to and finding none gives a "...couldn't find the requested page" pop-up instead. A more generally useful apprach is to place a voiding statement, "javascript:void(0)", after the "href=" to prevent the browser from attempting a redirect.

br>
Some things that can be accomplished with event handlers such as this require little change in the link statement. For example, choosing for convenience onMouseOver a message can be made to appear in the status-bar. Toggle between the "Look" and "Clear" links to see what is intended.

<a href="javascript:void(0)" onMouseOver="window.status='Message';">Look</a>Look

Clear


Care should be exercised in the type of use made of MouseOvers. One could choose to have a link such as this transfer the viewer to another page. Fine if at the end of a page, but hardly a benefit of any kind if the viewer is transported out of the page while in the middle of it. The code below will accomplish this, but will not be demonstrated. It is a good excercise. Besides, this section is still continuing.

<a href="" onMouseOver=
"parent.location='URL(link)';">Choice</a>


A pair of events related to Mouseovers and often seen in relation to form buttons on WebTV is presented below. These are Focus, where the cursor highlights the button, and Blur, when the cursor is removed. The event handler onFocus causes the original text to be replaced by another text. The onBlur event handler is used to return the original text.

<input type="button" value="Click Me"
onFocus="this.value='I Love It!!' "
onBlur="this.value='Click Me' ">
Start/Stop Here

Note: For PCs onMouseover and onMouseout replace onFocus and onBlur to have form buttons behave similarly.

Start/Stop Here



Images as well as text can be changed like this. No new event handlers have to be introduced. It is just a way of using a MouseOver. Since JS is being used, an image must at least initially be given a name:

<img src="URL.gif" name="first_image">


To make the image change to a new one the URL for the other must be known. For things of this type it is convenient to use:

<base href="stemURL">


The stem URL for WebTV users with Scrapbook images would be:

http://community.webtv.net/UserName/PageOfPics/scrapbookFiles/


Now all images used on this page can be referenced using just their end extension, e.g., importD23.gif or mailedD12.jpg.

In the MouseOver for an image swap, the beginning image and the following image are each referenced. For this, the image URL is made a variable. On each MouseOver the variable is changed to another variable, which is another image. Move the cursor between the "Rest" and the image. Wait a bit, for at times it will take a while for the image to load. Smaller images load much more quickly, and all will be made the same size as the original. The selection should therefore be made appropriately.

<script>
var start="";
var image1='URL1.gif';
var image2='URL2.gif';
var image3='URL3.gif';'
</script>
<a href="javascript:void(0)" onMouseOver=
"start=image1;
image1=image2;
image2=image3;
image3=start;
document.first_image.src=image1;">
<img width=? height=? src="URL1" name="first_image"></a>


Rest


It will be noted that each image swap is followed by a semicolon. This means that a JS statement is being carried out. With only a very slight modification, each image could be made clickable to a separate link.

JS Test-Bed 1 - - JS Test-Bed 2



<< PreviousNext >>