- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
Part one of this project took care of the "web services" portion of the project. Now let's make it work.
The TimeTracker client
At this point we have a client that retrieves data from the server and sends it back again after we've changed it. Now we need to create the actual application that accrues time.
Let's start by displaying the project. We want a collection of elements such as:
<div id="1" onclick="activateProject(this)">Personal (<span id="time1">302</span>/<span id="totaltime1">7204</span>)</div>
We'll create these div
s with a function:
... <script type="text/javascript" src="./json.js" ></script> <script type="text/javascript"> var projects = timesheet.projects; function displayProjects(){ for (i = 0; i < projects.length; i++) { document.write('<div id="'+i+'" onclick="activateProject(this)">'+ projects[i].project_name+ ' (<span id="time'+i+'">'+projects[i].time_this_period+ '</span>/<span id="totaltime'+i+'">'+projects[i].total_time+'</span>)</div>'); } } function prepareSaveForm(){ ... } function URLencode(sStr) { ... } </script> </head> <body> <h1>TimeTracker</h1> <div id="display"> <script type="text/javascript"> displayProjects(); </script> </div> <form name="saveForm" id="saveForm" action="savetimesheet.php" onsubmit="prepareSaveForm();" method="post"> ...
The first thing we're doing is extracting the array of projects from
the timesheet
object. We can then feed that into the
displayProjects()
function.
In the displayProjects()
function, we loop through the items in the array,
accessing the properties of the objects in the array.
The end result is a list of projects and the amount of time that's been spent on them.
Each project resides in a div
that fires an event when the user
clicks on it. At that point,
want to start accruing time and give users some sort of visual cue that his
or her action has had some effect.
Let's do that now:
... <script type="text/javascript" src="./json.js" ></script> <script type="text/javascript"> var projects = timesheet.projects; var currentProject = -1; var startTime = new Date(); function displayProjects(){ ... } function activateProject(clickedDiv){ projId = clickedDiv.id; if (currentProject != -1){ //Close current project } openProject(projId); } function openProject(projId){ startTime = new Date(); currentProject = projId; targetDiv = document.getElementById(projId); targetDiv.style.fontWeight = "bold"; } function prepareSaveForm(){ var returnObjectStr = JSON.stringify(timesheet); ...
The first thing we need to do is create variables that track the current state. In this case, that's the current project and the time the user started working on the current project.
When the user clicks a project, the browser executes the activateProject()
function. That function first checks to see if there's already a
project open -- we'll deal with closing projects in a moment -- and then opens the
project the user clicked.
The openProject()
function first sets the startTime
to
the current time, and the currentProject
to the project that's been clicked.
This
way, when we want to close the project, we know how much time to add to which project.
Finally, we get a reference to the actual div
element that's
been clicked and set it to bold.
Now let's look at closing the current project:
... function activateProject(clickedDiv){ projId = clickedDiv.id; if (currentProject != -1){ closeProject(currentProject); } openProject(projId); } function closeProject(projId){ currentTime = new Date(); elapsedTime = Math.floor((currentTime- startTime)/1000); thisProject = projects[projId]; timeSoFar = thisProject.time_this_period; thisProject.time_this_period = parseInt(timeSoFar) + parseInt(elapsedTime); thisProject.total_time = parseInt(thisProject.total_time) + parseInt(elapsedTime); } function openProject(projId){ ...
When the user chooses to activate a project, we first check to see whether there's already one open, and if there is, we close it.
To close a project, we first need to determine how much time has elapsed
since we opened it. We do that by subtracting the startTime
from the currentTime
. Because this will be in microseconds, we
need to divide by 1000 and round off the answer to get the number of
seconds. (In a production app, of course, we'd use minutes or hours, but
for the purpose of seeing results quickly, we'll use seconds.)
Once we know how much time has elapsed, we can get a reference to the specific
project object.
(Remember, the id
for the div
corresponds to the
project's index in the projects
array.) We can then add the elapsedTime
to the object's
time_this_period
, using the parseInt()
function to make sure the numbers are added as integers instead of strings.
Once we've updated the data, we need to update the display:
... function closeProject(projId){ ... thisProject.total_time = parseInt(thisProject.total_time) + parseInt(elapsedTime); targetDiv = document.getElementById(projId); targetDiv.style.fontWeight = "normal"; spanName = "time"+projId; targetSpan = document.getElementById(spanName); targetSpan.innerHTML = thisProject.time_this_period; spanName = "totaltime"+projId; targetSpan = document.getElementById(spanName); targetSpan.innerHTML = thisProject.total_time; } function openProject(projId){ ...
The first step is to set the font weight of the div
back to
normal
so it's no longer bold. Then we get references to the
span
s that represent the time and the total time, and update their values.
Finally, we need to take care of the final preparations before saving the data:
... ... function prepareSaveForm(){ if (currentProject != -1){ closeProject(currentProject); } timesheet.projects = projects; var returnObjectStr = JSON.stringify(timesheet); //saveForm.elements["timesheet"].value = URLencode(returnObjectStr); document.getElementById("timesheetValue").setAttribute("value", URLencode(returnObjectStr)); } ...
First, we close any open projects, and then we update the
timesheet
object with the
projects
array we've been using all this time.
We can then send the timesheet
object to the server
using JSON, as before.
If you reload the timesheet, you should see the updated values.