CIS166AA - JavaScript Glendale Community College
6000 West Olive Avenue
Glendale, AZ 85302 623.845.3000
   

 
   
 

 


 

 

 
Lesson 09
 
Lesson 9 - Reading

REQUIRED:

  • Read and do the practice work in Chapter 12 and Chapter 13
Lesson 9 - Notes and Practice Work

Chapter 12

Creating a Cookie

<script>

expireDate = new Date
expireDate.setMonth(expireDate.getMonth()+6)

userName = ""
if (document.cookie != "") {
userName = document.cookie.split("=")[1]
}

function setCookie() {
userName = document.myForm.nameField.value
document.cookie = "userName="+userName+";expires=" + expireDate.toGMTString()
}

</script>

<body onload="document.myForm.nameField.value = userName">
<form name="myForm" action="#">
<h1>Enter your name: <input type="text" name="nameField" onblur="setCookie()"></h1>
</form>

</body>

Reading a Cookie

<body>
<h1>
<script>

if (document.cookie != "") {
document.write("Hello, "+ document.cookie.split("=")[1])
}

</script>
</h1>
</body>

Listing Cookies

<body bgcolor="#FFFFFF">
<h2>
<script>

if (document.cookie == "") {
document.write("There are no cookies here")
}
else {
thisCookie = document.cookie.split("; ")

for (i=0; i<thisCookie.length; i++) {
document.write("Cookie name is '"+thisCookie[i].split("=")[0])
document.write("', and the value is '"+thisCookie[i].split("=")[1]+"'<br>")
}
}

</script>
</h2>
</body>

Using Cookies as a Counter

<script>

expireDate = new Date
expireDate.setMonth(expireDate.getMonth()+6)
hitCt = parseInt(cookieVal("pageHit"))
hitCt++
document.cookie = "pageHit="+hitCt+";expires=" + expireDate.toGMTString()

function cookieVal(cookieName) {
thisCookie = document.cookie.split("; ")
for (i=0; i<thisCookie.length; i++) {
if (cookieName == thisCookie[i].split("=")[0]) {
return thisCookie[i].split("=")[1]
}
}
return 0
}
</script>

<body bgcolor="#FFFFFF">
<h2>
<script>

document.write("You have visited this page " + hitCt + " times.")

</script>
</h2>
</body>

Deleting Cookies

<body bgcolor="#FFFFFF">
<script >

if (document.cookie != "") {
if (confirm("Do you want to delete the cookies?")) {
thisCookie = document.cookie.split("; ")
expireDate = new Date
expireDate.setDate(expireDate.getDate()-1)

for (i=0; i<thisCookie.length; i++) {
cookieName = thisCookie[i].split("=")[0]
document.cookie = cookieName + "=;expires=" + expireDate.toGMTString()
}
document.write("<h1>Number of cookies deleted: " + thisCookie.length + "<\/h1>")
}
}
</script>
</body>
</html>

Handling Mulitple Cookies

<html >
<script >

now = new Date
expireDate = new Date
expireDate.setMonth(expireDate.getMonth()+6)

hitCt = parseInt(cookieVal("pageHit"))
hitCt++
lastVisit = cookieVal("pageVisit")
if (lastVisit == 0) {
lastVisit = ""
}

document.cookie = "pageHit="+hitCt+";expires=" + expireDate.toGMTString()
document.cookie = "pageVisit="+now+";expires=" + expireDate.toGMTString()

function cookieVal(cookieName) {
thisCookie = document.cookie.split("; ")
for (i=0; i<thisCookie.length; i++) {
if (cookieName == thisCookie[i].split("=")[0]) {
return thisCookie[i].split("=")[1]
}
}
return 0
}

</script>

<body bgcolor="#FFFFFF">
<h2>
<script>

document.write("You have visited this page " + hitCt + " times.")
if (lastVisit != "") {
document.write("<br>Your last visit was " + lastVisit)
}


</script>
</h2>
</body>
</html>


Chapter 13

CSS

A page with no Style

<body bgcolor="#FFFFFF">
<blockquote>
<em>Enter KATHARINA</em>
</blockquote>
PETRUCHIO
<blockquote>
Good morrow, Kate; for that's your name, I hear.
</blockquote>
KATHARINA
<blockquote>
Well have you heard, but something hard of hearing:
They call me Katharina that do talk of me.
</blockquote>
PETRUCHIO
<blockquote>
You lie, in faith; for you are call'd plain Kate,<br>
And bonny Kate and sometimes Kate the curst;<br>
But Kate, the prettiest Kate in Christendom<br>
Kate of Kate Hall, my super-dainty Kate,<br>
For dainties are all Kates, and therefore, Kate,<br>
Take this of me, Kate of my consolation;<br>
Hearing thy mildness praised in every town,<br>
Thy virtues spoke of, and thy beauty sounded,<br>
Yet not so deeply as to thee belongs,<br>
Myself am moved to woo thee for my wife.
</blockquote>
KATHARINA
<blockquote>
Moved! in good time: let him that moved you hither<br>
Remove you hence: I knew you at the first<br>
You were a moveable.
</blockquote>
</body>

Adding Style

<head>
<style type="text/css">
body {font-weight: bold}
blockquote {font-weight: normal}
</style>
</head>

Using Class

<head>
<title>Taming of the Shrew</title>
<style type="text/css">
body {margin-left: 3em}
.character {font-weight: bold; margin-left: -2em; margin-bottom: 1em; margin-top: 1.5em}
.directions {font-style:italic}
</style>
</head>
<body bgcolor="#FFFFFF">
<div class="directions">
Enter KATHARINA
</div>
<div class="character">
PETRUCHIO
</div>
Good morrow, Kate; for that's your name, I hear.
<div class="character">
KATHARINA
</div>
Well have you heard, but something hard of hearing:<br>
They call me Katharina that do talk of me.
<div class="character">
PETRUCHIO
</div>
You lie, in faith; for you are call'd plain Kate,<br>
And bonny Kate and sometimes Kate the curst;<br>
But Kate, the prettiest Kate in Christendom<br>
Kate of Kate Hall, my super-dainty Kate,<br>
For dainties are all Kates, and therefore, Kate,<br>
Take this of me, Kate of my consolation;<br>
Hearing thy mildness praised in every town,<br>
Thy virtues spoke of, and thy beauty sounded,<br>
Yet not so deeply as to thee belongs,<br>
Myself am moved to woo thee for my wife.
<div class="character">
KATHARINA
</div>
Moved! in good time: let him that moved you hither<br>
Remove you hence: I knew you at the first<br>
You were a moveable.
</body>
</html>

Changing Fonts

<style type="text/css">
body {font-family: verdana, helvetica, arial, sans-serif; font-size: 10pt; margin-left: 3em}
.character {font-weight: bold; margin-left: -2em; margin-bottom: 1em; margin-top: 1.5em}
.directions {font-style:italic}
</style>

Using ID

<style type="text/css">
body {font-family: verdana, helvetica, arial, sans-serif; font-size: 10pt; margin-left: 3em}
.character {font-weight: bold; margin-left: -2em; margin-bottom: 1em; margin-top: 1.5em}
.directions {font-style:italic}
#week2 {background-color: #FFFF00}
</style>

<div class="character" id="week2">
KATHARINA
</div>

Using Span

<style type="text/css">
body {font-family: verdana, helvetica, arial, sans-serif; font-size: 10pt; margin-left: 3em}
.character {font-weight: bold; margin-left: -2em; margin-bottom: 1em; margin-top: 1.5em}
.directions {font-style:italic}
.gloss {background-color: #00FFFF}
</style>

You lie, in faith; for you are call'd plain Kate,<br>
And bonny Kate and <span class="gloss">sometimes</span> Kate the curst;<br>

Using Hover

<style type="text/css">
body {font-family: verdana, helvetica, arial, sans-serif; font-size: 10pt; margin-left: 3em}
.character {font-weight: bold; margin-left: -2em; margin-bottom: 1em; margin-top: 1.5em}
.directions {font-style:italic}

a {text-decoration: none}
a:visited {color: #000000}
a:hover {color: #FF0000; font-size:12pt}
</style>


<a href=#>Click Here!</a>


Lesson 9 - Assignment

  1. Create a single HTML file that contains the following:

    • Use cookies to display how many times a user has viewed the document
    • Use cookies to display the last time & date the user visited
    • A link to delete the cookies
    • 3 examples of CSS
  2. Save the file as CIS166AA09.HTM
  3. Upload the file and any images used to your CIS166aa folder on Gecko.

Send in Your Assignment to be Graded

After you have read these instructions, click on the link "Submit my assignment for grading" below. This will create a message for you to use to send in your assignment to the CIS166AA grader. It already will have the message address and the subject line filled in. DO NOT CHANGE THE ADDRESS OR THE SUBJECT LINE. Enter the following required information as the message text:

  • your full name
  • your palette account username
    • If you do not know your palette username, use your student ID card at a palette account lookup station in HTC1 or HTC2. If you are off campus, call the Student Help Desk at 623.845.HELP (4357) or send an email message to student-helpdesk@student.gc.maricopa.edu to request your palette username.
  • you email address
  • course section number
  • Include the full URL to lesson 9
    ( ie. http://gecko.gc.maricopa.edu/~palette_username/cis166aa/cis166aa09.htm )

Only by following this process can you be guaranteed that your assignment will get graded and the score recorded correctly.

Submit my assignment for grading

 


  Back to Top