2. JavaScript Quick Reference Card1.03
Escape Sequences { alert(num); ownerDocument – take a guess
n - new line, r - carriage return, t – tab character, num--;} style – CSS style declaration
- character, ' - apostrophe, '' - quote } tagName – element tag type. Curiously, always in
uNNNN – Unicode character at NNNN uppercase
e.g. u25BA gives the character ► function doLoop(num){ textContent – the Firefox equivalent of innerText
JavaScript in HTML do{ location Object
External JavaScript alert(num); host – URL of the site serving up the document
num--; href – the entire URL to the document
<script type=”text/javascript” defer=”defer”
src=”/scripts/explainthat.js”></script> }while (num > 0); pathname – the path to the document on the host
Inline JavaScript } protocol – the protocol used, e.g. http
<script type=”text/javascript”> reload(p) - reload the document. From the cache if p is
//your code here function forLoop(num){ true.
</script> replace(url) – replace the current document with the
var i;
Comments one at url. Discard document entry in browser history.
for (i=0;i<num;i++){
/* Comments spanning multiple lines */ screen Object
alert(num);
// Simple, single line, comment height – screen height in pixels
}
Conditional Execution width – screen width in pixels
}
if (Condition) CodeIfTrue;else CodeIfFalse4 window Object
Multiline CodeIf# must be placed in braces, {} alert(msg) – displays a dialog with msg
break causes immediate termination of the loop.
switch (variable) clearInterval(id) – clears interval id set by setInterval
loop statements after continue are skipped and the next
{ execution of the loop is performed. clearTimeout(id) – clears timeout id set by setTimeout
case Value1:Code; confirm(msg) – shows a confirmation dialog
break; function forInLoop(){ print() - prints the window contents
case Value2:Code; var s,x; prompt(msg,[default]) – shows prompt dialog,
break; optionally with default content. Returns content or null.
for (x in document)
..... setInterval(expr,interval) – sets repeat at interval ms.
{ The function expr is evaluated5 .
default:Code; s=x + ' = ' + document[x]; setTimeout(expr,time) Like setInterval but non-
} alert(s); repeating. 5
variable can be boolean, number, string or even date. } Notes
(condition)?(CodeIfTrue):(CodeIfFalse) }
1
Evaluates after use 2 Evaluates before use
Parentheses are not necessary but advisable
3
Zero-fill right shift 4 Note the semicolon!
This code is best tested in Opera which offers the option 5
Error Handling Passing arguments to function calls via expr is not well
of stopping the script at each alert. In place of
supported.
Method 1:The onerror event document any JavaScript object or an array can be
used to loop through its properties/elements. Color Coding
<script type=”text/javascript”>
return italics – user code blue – JavaScript Keywords
function whenError(msg,url,lineNo){
//use parameters to provide meaningful messages return causes immediate termination of the JavaScript red – Option object – JavaScript DOM object
function. If no value is returned, or if return is missing green – only numeric values blue - object properties
} the function return type is undefined.
green – object methods magenta – object events
window.onerror = whenError document Object Tested with Internet Explorer 6+, Firefox 1.5+ & Opera
</script> body - the body of the document 9.1+.
Place this code in a separate <script>..</script> tag cookie - read/write the document cookies
pair to trap errors occurring in other scripts. This
technique blocks errors without taking corrective action. domain – where was the document served from?
forms[] - array of all forms in the document
images[] - array of all images in the document
Method 2:The try..catch..finally statement
referrer – who pointed to this document?
function showLogValue(num){
URL – the URL for the document
var s = 'No Error';
getElementById(id) – element bearing ID of id
try
getElementsByName(n) – array of elements named n
{if (num < 0) throw 'badnum';
getElementsByTagName(t) - array of t tagged
if (num == 0) throw 'zero'; }
elements
catch (err)
write – write plain or HTML text to the document
{ s = err;
onload – occurs when the document is loaded
switch (err) {
onunload – occurs when user browses away, tab is
case 'badnum':num = -num; closed etc.
break; Element Object
case 'zero':num = 1; By element we mean any HTML element retrieved using
break; } the document.getElementBy# methods.
} attributes – all element attributes in an array
[finally{ alert([s,Math.log(num)]);}] className – the CSS style assigned to the element
} id – the id assigned to the element
The finally block is optional. The two techniques can be innerHTML – HTML content of the element
used in concert. innerText – content of the element shorn of all HTML
Looping tags. Does not work in Firefox
function whileLoop(num){ offset# – element dimensions (# = Height/Width) or
while (num > 0) location(# = Left/Right) in pixels