Client-Side ECMAScript/JavaScript Programming
An Introduction with Examples in HTML |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
script
element in HTML can contain
ECMAScript/JavaScript statements or can refer to a URL
that contains such statements)document
objectdocument
Object:
getElementsByTagName()
returns a list
of descendants with the given tag namegetElementById()
returns the object
with the given ID (i.e., with an id
attribute
that matches the given unique ID).
operatorgetAttribute()
and
setAttribute()
methodsload
is fired after the document has been
parsed and all content (e.g., images) has been
retrievedunload
is fired when the user leaves
the documentmousemove
, mouseover
,
mouseout
mousedown
, mouseup
,
click
, dblclick
keydown
, keyup
,
keypress
focus
, blur
change
, forminput
, input
, invalid
onevent
attribute<button id="calculate" onclick="calculateBMI();">Calculate</button>
onevent
propertydocument.getElementById("calculate").onclick = calculateBMI;
body
)
that assigns the handlersthis
:
this
will refer to the generatorbutton.onclick = calculator.start;
,
when start()
is invoked, this
will refer to button
NOT to calculator
button.onclick = function(){calculator.start();};
addEventListener()
method (defined
in DOM 2)shouldCapture
(if true
the
handler captures the event;
if false
the event can bubble-up
from descendants)
removeEventListener()
with the
same parametershref
Element:
style
property-
characters but ECMAScript/JavaScript identifiers can't
so identifiers omit the -
characters and
use "camel case" (e.g., font-size
becomes fontSize
)float
) so the associated identifiers are
prefixed with css
(e.g., cssFloat
)
document
object has a stylesheets
collectionstylesheets
has a
cssRules
collectionstyle
propertydocument.styleSheets[0].cssRules[i].style.color = "blue";
document.write()
to insert HTML text
into the documentnode = document.getElementById("result");
node = parent.firstChild
node = parent.childNodes[i]
newTextNode = document.createTextNode("...");
parent.replaceChild(node, newTextNode);
or parent.appendChild(newTextNode);
node.data = "...";
or
node.nodeValue = "...";
node.innerHTML = "...";
Document
ObjectbaseURI
and URL
:
base
element and full URL of the document,respectivelydomain
:
lastModified
:
referrer
:
cookie
:
isFinite()
and isNaN()
parseFloat()
and parseInt()
Date.parse()
escape()()
and unescape()
Location
objects have methods
for parsing URLswindow.setTimeout(statement,millis)
identifier = window.setInterval(statement,millis);
window.clearInterval(identifier);