-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter11-BrowserEvents.html
172 lines (148 loc) · 5.4 KB
/
chapter11-BrowserEvents.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<html>
<body>
<script type="text/javascript">
//Event Handlers
//Browser Javascript is single-threaded
//Registering a Handler
//Four event-related actions you might want:
//1. Registering an event handler
//2. Getting the event object
//3. Extracting information from this object
//4. Signaling that an event has been handled
//Assuming "button" holds a DOM node for a button:
//In IE
button.attachEvent("onclick", function(){alert("Click!");});
//Other browsers
button.addEventListener("click", function(){print("Click!");}, false);
//Setting the third parameter to "true" is rarely used because it can break in IE
//A function to determine which one to use
funciton registerEventHandler(node, event, handler) {
if (typeof node.addEventListener=="function") {
node.addEventListener(event, handler, false);
}
else {
node.attachEvent("on"+event, handler);
}
}
registerEventHandler(button, "click", function(){print("Click 2");});
//Unregister/Remove events
function unregisterEventHandler(node, event, handler) {
if(typeof node.removeEventListener=="function") {
node.removeEventListener(event, handler, false);
}
else {
node.detachEvent("on"+event, handler);
}
}
//Event Objects
registerEventHandler(document.body, "click", function(event){
event = event || window.event;
print(event.clientX, ",", event.clientY);
});
//Mouse-Related Event Types
//mousedown, mouseup, click, dblclick
//Event objects have property "target" or "srcElement"
//mousemove, mouseover, mouseout
registerEventHandler(myParagraph, "mouseover", function(event) {
event= event || window.event;
if ((event.target || event.srcElement) == myParagraph) {
print("Thouse mouse has entered my paragraph!");
}
});
//Keyboard Events
//Every keystroke: keydown, keyup, keypress
//Do not use keypress first, as not all keys are supported for keypress like IE's arrow keys
//Use the "keycode" property to find which key was pressed
//Also use: shiftKey, ctrlKey, altKey, metaKey
//The below code will cause a character to be printed whenever
//a key that makes that character is pressed
registerEventHandler(document.body, "keypress", function(event){
event = event || window.event;
var charCode = event.charCode || event.keyCode;
if(charCode) {
print("Character '", String.fromCharCode(charCode),"' was typed.");
}
});
//Stopping an event
//"stopPropogation()" used to stop event bubbling
//"preventDefault()"
//In IE, set "cancelBubble"=true
//"returnValue"=false
//Normalizing Event Objects
/* The following function goes over all the event object properties and
makes sure each one can be found under a standard name.
*/
function normalizeEvent(event) {
if(!event.stopPropogation) {
event.stopPropogation=function() {
this.cancelBubble=true;
};
event.preventDefault=function() {
this.returnValue=false;
};
}
if(!event.stop) {
event.stop=function() {
this.stopPropogation();
this.preventDefault();
};
}
if(event.srcElement && !event.target) {
event.target=event.srcElement;
}
if((event.toElement || event.fromElement) && !event.relatedTarget) {
event.relatedTarget=event.toElement || event.fromElement;
}
if(event.clientX != undefined && event.pageX ==undefined) {
event.pageX=event.clientX+document.body.scrollLeft;
event.pageY=event.clientY+document.body.scrollTop;
}
if(event.type=="keypress") {
event.character=String.fromCharCode(event.charCode || event.keyCode);
}
return event;
}
//With this function, we can write more convenient wrappers for "registerEventHandler"
//and "unregisterEventHandler"
function addHandler(node, type, handler) {
function wrapHandler(event) {
handler(normalizeEvent(event || window.event));
}
registerEventHandler(node, type, wrapHandler);
return {node: node, type: type, handler: wrapHanlder};
}
function removeHandler(object) {
unregisterEventHandler(object.node, object.type, object.handler);
}
//An example of this used could be stopping the letter "Q"
var blockQ=addHandler(textfield, "keypress", function() {
if(event.character.toLowerCase()=="q") {
event.stop();
}
});
removeHandler(blockQ);
//Tracking Focus
//The following code would cause the background of a text input field to be yellow as long as it is focused
addHandler(textfield, "focus", function(event) {
event.target.style.backgroundColor="yellow";
});
addHandler(textfield, "blur", function(event) {
event.target.style.backgroundColor="";
});
//Form Events
//Each changeable form element is capable of firing "change" events
//When content or value of input has changed
//Note that for some inputs, such as text, it does not fire until element is unfocused
addHandler(textfield, "change", function(event) {
print("Content of text field changed to '", event.target.value, "'.");
});
//Forms have a submit event which is fired when they submit
//This gives us a much better way to do form validation
//Register a submit handler, whcih stops the event when the content of the form is not valid
//This way, when user does not have Javscript enabled, the form works still without instant validation
//Window Events
//"load" event fires when document is fully loaded
//Whenever the document is scrolled, the browser fires a "scroll" event on window object
</script>
</body>
</html>