Web Assignment 2
RISP 523, Spring 2006
You are to add Javascript code to your index page that will prompt for the users name (first time only), then display their name and the number of times they have visited your index. page. You will need to do this using a predefined cookies.js file that you will also add to your web site. (2.5 points). Once you have this working, add a request for a second piece of user information on another page within your site, using an approach similiar to steps 2 and 3 below, alternating the code as necessary. Ask for, record in a cookie and then display when they return some type of information appropriate to your site. (2.5 points).
1) Copy this file (cookies.js) to your website
2) Insert this single line of code in your index.html file after the title section within the header section, as follows - ie - <SCRIPT LANGUAGE= , etc:
<HEAD>
<TITLE>cookie.js Example</TITLE>
<SCRIPT LANGUAGE="JavaScript" SRC="cookies.js"></SCRIPT>
</HEAD>
3) Add this code to your index page, somewhere in the body section:
<SCRIPT LANGUAGE="JavaScript">
<!--
if (GetCookie('user_id') == null) {
var getName = prompt('Hi... I see this is your first visit to my site. Please
enter your name.', '');
document.writeln('<H2>Welcome, ' + (getName != '' ? getName : 'Anonymous
user') + '</H2>' +
'This is your first visit.');
SetCookie('user_id', (getName != '' ? getName : 'Anonymous user'), expiry);
SetCookie('hit_count', '2', expiry);
}
else {
var getName = GetCookie('user_id');
var getHits = GetCookie('hit_count');
document.writeln('<H2>Welcome back, ' + getName + '</H2>' +
'You have visited ' + getHits + ' times.');
getHits = parseInt(getHits) + 1;
SetCookie('hit_count', '' + getHits + '', expiry);
}
//-->
</SCRIPT>