Types (Strings, Numbers, Arrays)
Conditionals, Loops, Functions
Modular JS Pattern
Basic Event Handlers
Today: More Events, Forms, Timers, and Animations
"use strict";
...your code...
Writing "use strict";
at the very top of your JS file turns
on strict syntax checking:
You should always turn on strict mode for your code in this class!
"use strict";
(function() {
// set-up code that doesn't involve the DOM
// (e.g. setting up initial values, arrays, etc.)
window.onload = function() {
// phew! your code goes here
};
//function definitions go here
})();
Used to delay or set intervals for executing functions
method | description |
---|---|
setTimeout(function, delayMS) | arranges to call given function after given delay in ms |
setInterval(function, delayMS) | arranges to call function repeatedly every delayMS ms |
clearTimeout(timerID)
clearInterval(timerID) |
stops the given timer |
Both setTimeout
and setInterval
return an ID
representing the timer
clearTimeout
/Interval
later to
stop the timersetTimeout
Example
window.onload = function() {
document.getElementById("clickme").onclick = delayedMessage;
};
function delayedMessage() {
document.getElementById("outputText").innerHTML = "Wait for it...";
setTimeout(sayBooyah, 5000);
}
function sayBooyah() { // called when the timer goes off
document.getElementById("outputText").innerHTML = "BOOYAH!";
}
setInterval
Example
let timer = null; // stores ID of interval timer
function delayMsg2() {
timer = setInterval(mowgli, 1000);
}
function mowgli() {
document.getElementById("outputText").innerHTML += "Mowgli!"
}
clearInterval
Example
let timer = null; // stores ID of interval timer
function toggleDelayMessage() {
if (timer === null) {
timer = setInterval(mowgli, 1000);
} else {
clearInterval(timer);
timer = null;
}
}
function mowgli() {
document.getElementById("outputText").innerHTML += "Mowgli!"
}
function delayedMultiply() {
// 6 and 7 are passed to multiply when timer goes off
setTimeout(multiply, 2000, 6, 7);
}
function multiply(a, b) {
alert(a * b);
}
Any parameters after the delay are eventually passed to the timer function
Why not just write this?
setTimeout(multiply(6, 7), 2000);
Many students mistakenly write ()
when passing the function
setTimeout(booyah(), 2000);
setTimeout(booyah, 2000);
setTimeout(multiply(num1, num2), 2000);
setTimeout(multiply, 2000, num1, num2);
What does it actually do if you have the ()
?
<input>
yes/no choices that can be checked and unchecked (inline)
Lettuce
Tomato
Pickles
None, 1, or many checkboxes can be checked at same time
When sent to server, any checked boxes will be sent with value on:
http://webster.cs.washington.edu/params.php?tomato=on&pickles=on
Use checked="checked"
attribute in HTML to initially check the box
<input>
sets of mutually exclusive choices (inline)
Visa
MasterCard
American Express
Grouped by name attribute (only one can be checked at a time)
Must specify a value for each one
<label>
associates nearby text with control, so you can click text to activate control
can be used with checkboxes or radio buttons
label element can be targeted by CSS style rules
<select>
,
<option>
menus of choices that collapse and expand (inline)
Option element represents each choice
Select optional attributes: disabled, multiple, size
Optional selected attribute sets which one is initially chosen
Optional multiple attribute allows selecting multiple items with shift- or ctrl-click
Must declare parameter's name with []
if you allow multiple selections
Option tags can be set to be initially selected
What should we do if we don't like the bold appearance of the optgroups?
<fieldset>
,
<legend>
groups of input fields with optional caption (block)
Fieldset groups related input fields, adds a border; legend supplies a caption
element[attribute="value"] {
property: value;
property: value;
...
property: value;
}
input[type="text"] {
background-color: yellow;
font-weight: bold;
}
Attribute selector: matches only elements that have a particular attribute value
Useful for controls because many share the same element (input)
innerHTML
Property
Hello
function addText() {
let span = document.getElementById("output");
span.innerHTML += " ... goodbye";
}
Can change the text inside most elements by setting the innerHTML
property
innerHTML
// bad style!
let paragraph = document.getElementById("welcome");
paragraph.innerHTML = "text and <a href=\"page.html\">link</a>";
innerHTML
can inject arbitrary HTML content into page
However, this is prone to bugs and errors and is considered poor style
We forbid using innerHTML
to inject HTML tags; inject plain text only
Every JavaScript program can refer to the following global objects:
method | 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 |