Attribution: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.
Otherwise noted: Claude Anderson was given permission to modify the slides for CSSE 290 at Rose-Hulman by author Jessica Miller.
The authors' original slides, based on Web Programming Step by Step, can be seen at http://webstepbook.com.
Some of the examples in some days' slides are from David Fisher at Rose-Hulman, who was kind enough to allow me to use them.
My intention is to mark these examples with [DSF].
My note: BEGIN FROM: Alice Smith (alice@example.com) TO: Robert Jones (roberto@example.com) SUBJECT: Tomorrow's "Birthday Bash" event! MESSAGE (english): Hey Bob, Don't forget to call me this weekend! PRIVATE: true END
<?xml version="1.0" encoding="UTF-8"?> <note private="true"> <from>Alice Smith (alice@example.com)</from> <to>Robert Jones (roberto@example.com)</to> <subject>Tomorrow's "Birthday Bash" event!</subject> <message language="english"> Hey Bob, Don't forget to call me this weekend! </message> </note>
<element attribute="value">content</element>
h1
, div
, img
, etc.id
/class
, src
, href
, etc.<?xml version="1.0" encoding="UTF-8"?> <!-- XML prolog --> <note private="true"> <!-- root element --> <from>Alice Smith (alice@example.com)</from> <to>Robert Jones (roberto@example.com)</to> <subject>Tomorrow's "Birthday Bash" event!</subject> <message language="english"> Hey Bob, Don't forget to call me this weekend! </message> </note>
<?xml ... ?>
header tag (prolog)note
)<measure number="1"> <attributes> <divisions>1</divisions> <key><fifths>0</fifths></key> <time><beats>4</beats></time> <clef> <sign>G</sign><line>2</line> </clef> </attributes> <note> <pitch> <step>C</step> <octave>4</octave> </pitch> <duration>4</duration> <type>whole</type> </note> </measure>
book
, title
, author
key
, pitch
, note
new Ajax.Request("url", { method: "get", onSuccess: functionName }); ... function functionName(ajax) { do something with ajax.responseXML; }
ajax.responseText
contains the data in plain textajax.responseXML
is a parsed XML DOM tree object<?xml version="1.0" encoding="UTF-8"?> <categories> <category>children</category> <category>computers</category> ... </categories>
To get a list of all nodes that use a given element:
var elms = node.getElementsByTagName("tag");
To get the text inside of a node:
var text = node.firstChild.nodeValue;
To get an attribute's value from a node:
var attrValue = node.getAttribute("name");
Can't get a list of nodes by id or class using $
or $$
:
var elms =$$("#main li");$("id");
Can't get/set the text inside of a node using innerHTML
:
var text =$("foo").innerHTML;
Can't get an attribute's value using .attributeName
:
var imageUrl =$("myimage").src;
nodeName
, nodeType
, nodeValue
, attributes
firstChild
, lastChild
, childNodes
, nextSibling
, previousSibling
, parentNode
getElementsByTagName
, getAttribute
, hasAttribute[s]
, hasChildNodes
appendChild
, insertBefore
, removeChild
, replaceChild
up
, down
, ancestors
, childElements
, or siblings
<?xml version="1.0" encoding="UTF-8"?> <employees> <lawyer money="99999.00" /> <janitor name="Ed"> <vacuum model="Hoover" /> </janitor> <janitor name="Bill">no vacuum, too poor</janitor> </employees>
// how much money does the lawyer make? var lawyer = ajax.responseXML.getElementsByTagName("lawyer")[0]; var salary = lawyer.getAttribute("money"); // "99999.00" // array of 2 janitors var janitors = ajax.responseXML.getElementsByTagName("janitor"); var vacModel = janitors[0].getElementsByTagName("vacuum")[0].getAttribute("model"); // "Hoover" var excuse = janitors[1].firstChild.nodeValue; // "no vacuum, too poor"
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year><price>30.00</price> </book> <book category="computers"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <year>2003</year><price>49.99</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year><price>29.99</price> </book> <book category="computers"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year><price>39.95</price> </book> </bookstore>
Let's write some code for this example
animalgame.php
.<node nodeid="id"> <question>question text</question> <yes nodeid="id" /> <no nodeid="id" /> </node>
<node nodeid="id"> <answer>answer text</answer> </node>
animalgame.php?nodeid=id
nodeid
of 1
to get the first questionQuestions we should ask ourselves:
responseXML
in Firebug<!DOCTYPE html>
tag)JavaScript Object Notation (JSON): Data format that represents data as a set of JavaScript objects
var person = { name: "Philip J. Fry", // string age: 23, // number "weight": 172.5, // number friends: ["Farnsworth", "Hermes", "Zoidberg"], // array getBeloved: function() { return this.name + " loves Leela"; } }; alert(person.age); // 23 alert(person["weight"]); // 172.5 alert(person.friends[2])); // Zoidberg alert(person.getBeloved()); // Philip J. Fry loves Leela
this
.fieldName
or ["fieldName"]
syntaxweight
above)<?xml version="1.0" encoding="UTF-8"?> <note private="true"> <from>Alice Smith (alice@example.com)</from> <to>Robert Jones (roberto@example.com)</to> <to>Charles Dodd (cdodd@example.com)</to> <subject>Tomorrow's "Birthday Bash" event!</subject> <message language="english"> Hey guys, don't forget to call me this weekend! </message> </note>
{ "private": "true", "from": "Alice Smith (alice@example.com)", "to": [ "Robert Jones (roberto@example.com)", "Charles Dodd (cdodd@example.com)" ], "subject": "Tomorrow's \"Birthday Bash\" event!", "message": { "language": "english", "text": "Hey guys, don't forget to call me this weekend!" } }
method | description |
---|---|
JSON.parse(string)
|
converts the given string of JSON data into an equivalent JavaScript object and returns it |
JSON.stringify(object)
|
converts the given object into a string of JSON data (the opposite of JSON.parse )
|
JSON.parse
on it to convert it into an objectvar data = JSON.parse(ajax.responseText);
{ "window": { "title": "Sample Widget", "width": 500, "height": 500 }, "image": { "src": "images/logo.png", "coords": [250, 150, 350, 400], "alignment": "center" }, "messages": [ {"text": "Save", "offset": [10, 30]} {"text": "Help", "offset": [ 0, 50]}, {"text": "Quit", "offset": [30, 10]}, ], "debug": "true" }
Given the JSON data at right, what expressions would produce:
var title = data.window.title; var coord = data.image.coords[2]; var len = data.messages.length; var y = data.messages[len - 1].offset[1];
Suppose we have a service books_json.php
about library books.
{ "categories": ["computers", "cooking", "finance", ...] }
category
query parameter to see all books in one category: { "books": [ {"category": "cooking", "year": 2009, "price": 22.00, "title": "Breakfast for Dinner", "author": "Amanda Camp"}, {"category": "cooking", "year": 2010, "price": 75.00, "title": "21 Burgers for the 21st Century", "author": "Stuart Reges"}, ... ] }
Write a page that processes this JSON book data.
function showBooks(ajax) { // add all books from the JSON data to the page's bulleted list var data = JSON.parse(ajax.responseText); for (var i = 0; i < data.books.length; i++) { var li = document.createElement("li"); li.innerHTML = data.books[i].title + ", by " + data.books[i].author + " (" + data.books[i].year + ")"; $("books").appendChild(li); } }
eval
function// var data = JSON.parse(ajax.responseText); var data = eval(ajax.responseText); // don't do this! ...
eval
keyword that takes a string and runs it as codeJSON.parse
does,JSON.parse
filters out potentially dangerous code; eval
doesn'teval
is evil and should not be used!