SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 90 | Next

Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb

"Accelerated DOM Scripting with Ajax, APIs, and Libraries"

getElementById("passcode");
if(!passcode.regexp.test(passcode.value))
{
alert('Not a valid passcode');
evt.preventDefault();
}
}
addListener(document.getElementById("frm"), 'submit', isPasscodeValid);
Notice that the function is expecting a parameter. The DOM actually passes in the event
object as the first parameter, which is declared as evt.
An event object stores various properties and methods about the event, such as what type
of event, which mouse button was pressed (or which keys on the keyboard were pressed if it??™s
a keyboard event), and what the target element is.
CHAPTER 2 n HTML, CSS, AND JAVASCRIPT 48
Now you have to do something different for IE. Versions older that IE version 6 don??™t pass
in the event object as a parameter, but instead have an event object at the window level that
you can access. In addition, IE??™s event object doesn??™t recognize preventDefault(); instead, it
requires use of the property returnValue.
Let??™s update the function to work in IE:
function isPasscodeValid(evt)
{
evt = evt||window.event;
var passcode = document.getElementById('passcode');
if(!passcode.regexp.test(passcode.value))
{
alert('Not a valid passcode');
if(evt.preventDefault)
{
evt.preventDefault();
}else{
evt.returnValue=false;
}
}
}
Here??™s a good example of the object detection described earlier. Because you don??™t know
which browser is executing the code, you have to test for the existence of objects or methods
before using them (shown in two different ways in the example).


Pages:
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
Free English grammar and study guid Russian bride druga wojna światowa counter strike 1.6 hotel jelenia góra