By the end of today, you will be able to:
We've seen event binding before, when we first introduced events and
event handlers. There are two ways to have a function,
say fred
, be invoked when a button is clicked:
<button type="button" onClick="fred();"> <script> function fred () { ... } </script>
<button type="button" id="doit"> <script> function fred () { ... } document.querySelector("#doit").onclick = fred; </script>
(For you pedants, these are not exactly the same, but for our purposes they are.)
Advantages of the new way:
var elts = document.querySelectorAll("#mylist > li"); for( var i=0; i < elts.length; i++ ) { var li_elt = elts[i]; li_elt.onclick = fred; }
List of reasons:
cool
shiny
new
modern
The fancy feature where I can click on a quiz answer and make it disappear is done using event binding and a kind of compact notation like that loop above. If I only had inline event handlers, it would be so tedious that I simply wouldn't do it. With event binding, it's automagic.
Local Storage is a departure from most of our JavaScript because we are interacting with something outside the little world of JavaScript and the document.
Furthermore, it can be weird and hard to debug because restarting
the browser doesn't reset
everything. Before in this course, you
could always start over
by reloading the page, but no
longer. That's because local storage is sticky.
Method/ Property |
Usage Example |
---|---|
.length |
localStorage.length The number of items stored in the storage. |
.setItem() |
localStorage.setItem("zip", "02481") Stores an item as a pair of a key and value. |
.getItem() |
localStorage.getItem("zip") Retrieves the value stored under the key that is passed as an argument to the method. |
.clear() |
localStorage.clear() Removes all items stored in the local storage. |
.removeItem() |
localStorage.removeItem("zip") Removes only one item, whose key is passed as the argument. |
.key() |
localStorage.key(1) Retrieves the name of the key with index 1 (imagine that keys are stored in a array and we can access each of them through the index). |
The JavaScript on a page executes the following code:
var year = localStorage.getItem("year");
What value does year
have?
We want to keep a visits counter on a page, so we write the following code, but it doesn't do the right thing. Why not?
var numPrior = localStorage.getItem("numPriorVisits"); var numPrior = numPrior + 1; localStorage.setItem("numPriorVisits", numPrior);
putItem
parseInt
on the value.clear()
the localStorage firstLocalStorage is a lot like using global variables except ...
Suppose we have a DIV
on the page like this:
<div id="fred"> ... <div>
We want to have the doStuff
function executed whenever
Fred is clicked on. How can we do that?
<div id="fred" onclick="doStuff" > ... <div>
<div id="fred" onclick="doStuff();" > ... <div>
document.querySelector("#fred").onclick = doStuff();
document.querySelector("#fred").onclick = doStuff;
For accessiblity reasons, many professional websites have a way to increase or decrease the font size. We're going to do that.
First, try the following
Let's look at the code:
var fontSize=12; // in px function changeFontSize(diff) { fontSize = fontSize + diff; document.body.style.fontSize=fontSize+"px"; console.log("font set to "+fontSize); } function increase() { console.log("increasing"); changeFontSize(+1); } function decrease() { console.log("decreasing"); changeFontSize(-1); } function setup() { console.log("set up"); changeFontSize(0); document.querySelector('#bigger').onclick = increase; document.querySelector('#smaller').onclick = decrease; } window.onload = setup; // to work in jsFiddle, we need to invoke setup() ourselves, since there // is no "window" object (our code is inside the panes of jsFiddle).
What questions do you have?
Open the jsFiddle.
Alternatively:
Download the following files to your computer:
Do the following
getItem
returns null
if there is no
value.
Your solution might work like this:
We hope that after these activities you have a good understanding of: