Java Reference
In-Depth Information
The solution is surprisingly simple. You create a new Date object based on the date entered by the user.
Then you get the day of the week using the Date object's getDay() method. This returns a number, but
by defi ning an array of days of the week to match this number, you can use the value of getDay() as
the index to your days array.
You also do some basic sanity checking to make sure that the user has entered numbers and that in
the case of the date, it's between 1 and 31. You could have defi ned a select element as the method of
getting the date and only including numbers from 1 to 31. Of course, neither of these methods checks
whether invalid dates are entered (for example, the 31st of February). You might want to try this as an
additional exercise.
Hint: To get the last day of the month, get the fi rst day of the next month, and then subtract 1.
Chapter 11
Exercise 1 Question
Create a page that keeps track of how many times the page has been visited by the user in the last
month.
Exercise 1 Solution
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<script language=”JavaScript”
type=”text/JavaScript”
src=”CookieFunctions.js”></script>
<script type=”text/javascript”>
var pageViewCount = getCookieValue(“pageViewCount”);
var pageFirstVisited = getCookieValue(“pageFirstVisited”);
if (pageViewCount == null)
{
pageViewCount = 1;
pageFirstVisited = new Date();
pageFirstVisited.setMonth(pageFirstVisited.getMonth());
pageFirstVisited = pageFirstVisited.toGMTString();
setCookie(“pageFirstVisited”,pageFirstVisited,”“,”“)
}
else
{
pageViewCount = Math.floor(pageViewCount) + 1;
Search WWH ::




Custom Search