Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
document.cookie = "name=value";
document.cookie = "username=smith"; // setting two cookies document.cookie = "lastlogin=Dec 1 2045"; ... alert(document.cookie); "username=smith; lastlogin=Dec 1 2045"
document.cookie field (which is a magical string with odd behavior)document.cookie, it actually appends / concatenates a new cookie
= operator)
var cookies = document.cookie.split(";"); // ["username=smith", "lastlogin=Dec 1 2045"]
for (var i = 0; i < cookies.length; i++) {
var eachCookie = cookies[i].split("="); // ["username", "smith"]
var cookieName = eachCookie[0]; // "username"
var cookieValue = eachCookie[1]; // "smith"
...
}
document.cookie by breaking it apart by ; and then by =
// expire at a specific date/time document.cookie = "username=smith; expires=Fri, 16 Nov 2012 20:47:11 UTC"; // expire 7 days from now var date = new Date(); date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000)); // add 7 days document.cookie = "username=smith; expires=" + date.toGMTString();
expires attribute to tell the browser when to delete the cookieDate object to set an expiration relative to today's dateexpires value is set, the cookie expires at the end of the session
document.cookie = "username=; expires=Thu, 01-Jan-1970 00:00:01 GMT"; // delete
...
<!-- using the instructor-provided Cookie.js class -->
<script src="http://www.webstepbook.com/Cookie.js" type="text/javascript"></script>
Cookie.set("username", "smith");
// (later)
alert(Cookie.get("username")); // smith
set, get, exists, remove, and remember
localStorage["name"] = "value";
localStorage["username"] = "smith";
localStorage["lastlogin"] = "Dec 1 2045";
...
alert(localStorage["username"]); smith
localStorage associative array that remembers data on your page indefinitely
alert(localStorage["username"]); // smith
localStorage.removeItem("username");
localStorage.clear();
localStorage contents
localStorage has useful properties/methods:
length, clear, removeItem, key
sessionStorage["name"] = "value";
sessionStorage["favoritecolor"] = "blue"; sessionStorage["age"] = 19;
sessionStorage associative array is just like localStorage but the data expires when you end your session (when the browser or tab closes)
localStorage and sessionStorage are stored as strings; other types are implicitly converted to string on assignment. You must convert back to other types if necessary.
var name = openDatabase("dbName", "version", "description", size);
name.transaction(function(tx) {
tx.executeSql("query", [arguments], callback, errorCallback);
...
});
var db = openDatabase("myuw", "1.0", "UW Student Data", 2 * 1024 * 1024);
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE students (name, age)");
tx.executeSql("INSERT INTO students VALUES ('Bart', 10)");
tx.executeSql("INSERT INTO students VALUES ('Lisa', 8)");
});
db.readTransaction(function(tx) {
tx.executeSql("SELECT * FROM students", [], function(tx, result) {
// loop over the rows of results
for (var i = 0; i < result.rows.length; i++) {
alert("Student " + result.rows.item(i).name + " is age " + result.rows.item(i).age);
}
}); // Bart is age 10
}); // Lisa is age 8
transaction (to modify DB) or readTransaction (to read/select from DB)result object of rows that matched the query
localStorage and expand with [+]