3. “O wonder!
How many goodly creatures are there here! How
beauteous mankind is! O brave new world! That
has such people in't!”
- Shakespeare’s The Tempest
35. // Prevent type coercion
var sum = parseInt("5", 10) + 6 + 7; // 18
37. // Various "false" values
var nullVal = null;
var undefinedVal = undefined;
var zeroVal = 0;
var falseVal = false;
var emptyString = "";
// All would equal false in an if-clause
if (emptyString) {
// Would never go in here
}
41. // Scope - global or local
// Global
var quote = "I had run for 3 years, 2 months,
14 days, and 16 hours."
function () {
// Local
var pantherParty = "I'm sorry I had to
fight in the middle of your Black Panther
party.";
// Global
question = "And so, you just ran?";
}
42. // Global
function meetingJFK () {
var JFKQuestion = "Congratulations, how do
you feel?";
// Local
function forrestReply () {
return "I gotta pee.";
}
return forrestReply();
}
meetingJFK(); // I gotta pee
forrestReply(); // Error: not accessible
64. <video controls>
<source src="swedish-flag.mp4">
<source src="swedish-flag.ogv">
<p>
Sorry, your web browser doesn't
support the video element.
</p>
</video>
95. // Using JSON
var info = {
"language" : "Bulgarian",
"location" : "Veliko Tarnovo"
};
// Save as string
localStorage.setItem("info", JSON.stringify(info));
// Load as JSON object
alert(JSON.parse(localStorage.info));
97. // Use postMessage on a window to send a message
var iframeWin = document.getElementById("da-iframe").contentWindow;
iframeWin.postMessage("Love you!", "http://robertnyman.com");
98. // Handle message
function displayMessage (evt) {
var message;
if (evt.origin !== "http://robertnyman.com") {
message = "You are not worthy";
}
else {
message = "I got " + evt.data + " from " + evt.origin;
}
document.getElementById("received-message").innerHTML = message;
}
// Using onmessage to receive message
if (window.addEventListener) {
// For standards-compliant web browsers
window.addEventListener("message", displayMessage, false);
}
else {
window.attachEvent("onmessage", displayMessage);
}
104. if (window.addEventListener) {
/*
Works well in Firefox and Opera with the
Work Offline option in the File menu.
Pulling the ethernet cable doesn't seem to trigger it
*/
window.addEventListener("online", isOnline, false);
window.addEventListener("offline", isOffline, false);
}
else {
/*
Works in IE with the Work Offline option in the
File menu and pulling the ethernet cable
*/
document.body.ononline = isOnline;
document.body.onoffline = isOffline;
}