Home arrow JavaScript arrow Page 8 - Understanding The JavaScript Event Model (part 2)

Tonight's Menu - JavaScript

In this concluding article on the JavaScript event model, findout how the Event object can be used to do ever more complex things,including manipulating the dimensions of a Web page and tracking andintercepting keyboard and mouse events.

TABLE OF CONTENTS:
  1. Understanding The JavaScript Event Model (part 2)
  2. Back To Basics
  3. How's The Weather Up There?
  4. X Marks The Spot
  5. Enter The Stalker
  6. Of Keys And Clicks
  7. A Few Modifications
  8. Tonight's Menu
  9. Reducing The Crime Rate
  10. Endgame
By: Team Melonfire, (c) Melonfire
Rating: starstarstarstarstar / 32
July 10, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Next, let's look at a couple of examples that demonstrate the possible applications of this capability. This next example sets up a small menu containing multiple links, each link associated with a number. The user can select a specific link by pressing the appropriate number key on the keyboard.

Here's the code for Netscape Navigator:
<html>
<head>
<script language="JavaScript">
function menuSelect(e) 
{
// select 1
if(e.type == 'keydown' && e.which == 49)
{
document.location.href="link1.htm"
} 
// select 2
else if(e.type == 'keydown' && e.which == 50)
{
document.location.href="link2.htm"
}
// select 3
else if(e.type == 'keydown' && e.which == 51)
{
document.location.href="link3.htm"
}
// select 4
else if(e.type == 'keydown' && e.which == 52)
{
document.location.href="link4.htm"
}
}
document.onkeydown = menuSelect;
</script>
</head>
<body>
</body>
</html>
And here's the Internet Explorer equivalent:
<html>
<head>
<script language="JavaScript">
function menuSelect() 
{
// select 1
if(event.type == 'keydown' && event.keyCode == 49)
{
document.location.href="link1.htm";
} 
// select 2
else if(event.type == 'keydown' && event.keyCode == 50)
{
document.location.href="link2.htm";
}
// select 3
else if(event.type == 'keydown' && event.keyCode == 51)
{
document.location.href="link3.htm";
}
// select 4
else if(event.type == 'keydown' && event.keyCode == 52)
{
document.location.href="link4.htm";
}
}
document.onkeydown = menuSelect;
</script>
</head>
<body>
</body>
</html>
A quick note on something new here: in both cases, the "type" property has been used to identify the type of event generated. This makes it possible to take action selectively, on the basis of the specific type of event generated.

 
 
>>> More JavaScript Articles          >>> More By Team Melonfire, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVASCRIPT ARTICLES

- Javascript for Beginners: An Introduction
- Introduction to JavaScript
- Adding Elements to a Tree with TreeView jQue...
- Using the Persist Argument in a TreeView jQu...
- Using Unique and Toggle in a TreeView jQuery...
- Using Event Delegation for Mouseover Events ...
- Using the Animate Option in a Treeview jQuer...
- Using HTML Lists with Event Delegation in Ja...
- Opened and Closed Branches on a TreeView jQu...
- Mouseover Events and Event Delegation in Jav...
- Creating a TreeView JQuery Hierarchical Navi...
- Event Delegation in JavaScript
- A Look at the New YUI Carousel Control
- Working with Draggable Elements and Transpar...
- Displaying Pinned Handles with Resizable Con...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: