Except where otherwise noted, the contents of this presentation are Copyright 2010 Marty Stepp and Jessica Miller.
Every Javascript program can refer to the following global objects:
name | description |
---|---|
document
|
current HTML page and its content |
history
|
list of pages the user has visited |
location
|
URL of the current HTML page |
navigator
|
info about the web browser you are using |
screen
|
info about the screen area occupied by the browser |
window
|
the browser window |
window
objectthe entire browser window; the top-level object in DOM hierarchy
window
objectalert
,
confirm
,
prompt
(popup boxes)
setInterval
,
setTimeout
clearInterval
,
clearTimeout
(timers)
open
,
close
(popping up new browser windows)
blur
,
focus
,
moveBy
,
moveTo
,
print
,
resizeBy
,
resizeTo
,
scrollBy
,
scrollTo
window.open
window.open("http://foo.com/bar.html", "My Foo Window", "width=900,height=600,scrollbars=1");
window.open
pops up a new browser windowdocument
objectthe current web page and the elements inside it
location
objectthe URL of the current web page
navigator
objectinformation about the web browser application
navigator
object to see what browser is being used, and write browser-specific scripts and hacks:
if (navigator.appName === "Microsoft Internet Explorer") { ...
history
objectthe list of sites the browser has visited in this window
history
properties, for security
abort
|
blur
|
change
|
click
|
dblclick
|
error
|
focus
|
keydown
|
keypress
|
keyup
|
load
|
mousedown
|
mousemove
|
mouseout
|
mouseover
|
mouseup
|
reset
|
resize
|
select
|
submit
|
unload
|
click
event (onclick
) is just one of many events that can be handled
element.onevent = function; element.observe("event", function);
// call the playNewGame function when the Play button is clicked
$("play").observe("click", playNewGame);
observe
method (added by Prototype)
observe
substitutes for addEventListener
(not supported by IE)
event
object
function name(event) {
// an event handler function ...
}
method / property name | description |
---|---|
type
|
what kind of event, such as "click" or "mousedown"
|
element() *
|
the element on which the event occurred |
stop() **
|
cancels an event |
stopObserving()
|
removes an event handler |
srcElement
and which
properties
return false;
, stopPropagation
, etc.
click
|
user presses/releases mouse button on the element |
dblclick
|
user presses/releases mouse button twice on the element |
mousedown
|
user presses down mouse button on the element |
mouseup
|
user releases mouse button on the element |
mouseover
|
mouse cursor enters the element's box |
mouseout
|
mouse cursor exits the element's box |
mousemove
|
mouse cursor moves around within the element's box |
The event
passed to a mouse handler has these properties:
property/method | description |
---|---|
clientX , clientY
|
coordinates in browser window |
screenX , screenY
|
coordinates in screen |
offsetX , offsetY
|
coordinates in element (non-standard) |
pointerX() , pointerY() *
|
coordinates in entire web page |
isLeftClick() **
|
true if left button was pressed
|
pageX
and pageY
button
and which
<pre id="target">Move the mouse over me!</pre>
window.onload = function() { $("target").observe("mousemove", showCoords); }; function showCoords(event) { $("target").innerHTML = "pointer: (" + event.pointerX() + ", " + event.pointerY() + ")\n" + "screen : (" + event.screenX + ", " + event.screenY + ")\n" + "client : (" + event.clientX + ", " + event.clientY + ")"; }
Move the mouse over me!
this
(8.1.3)
this.fieldName // access field this.fieldName = value; // modify field this.methodName(parameters); // call method
window
object (so this
=== window
)
window
this
keyword refers to the current object
window.onload = function() { $("textbox").observe("mouseout", booyah); // bound to text box here $("submit").observe("click", booyah); // bound to submit button here }; function booyah() { // booyah knows what object it was called on this.value = "booyah"; }
this
(rather than the window
)
this
<fieldset> <label><input type="radio" name="ducks" value="Huey" /> Huey</label> <label><input type="radio" name="ducks" value="Dewey" /> Dewey</label> <label><input type="radio" name="ducks" value="Louie" /> Louie</label> </fieldset>
function processDucks() {if ($("huey").checked) { alert("Huey is checked!"); } else if ($("dewey").checked) { alert("Dewey is checked!"); } else { alert("Louie is checked!"); }alert(this.value + " is checked!"); }