Cookies and Objects Lab



Cookies and Objects Lab

in

JavaScript

Chapters 8 and 16 the JavaScript book

Web Programming

Fall 2005

Objects

Probably JavaScript’s biggest limitation, if the programmer is not careful, is due to its weak typing of datatypes. Thus, a great deal of what you code is difficult to determine what it is storing. In the case of variables you simply write:

var ;

Or you can just start using the variable right off in your code. Because there is no strong typing of variables in JavaScript, if we want a function to return a value we simply make sure there is a return included in the function. If there is no return, then the function is assumed not to return anything. Thus the function:

function print(msg)

might return a value or it might not. It all depends on if there is a return statement in the code for the function.

Creating Class Objects

Creating our own class objects in JavaScript is also a confusing process. First, the declaration for an object class in JavaScript is the exact same as the declaration for a function. Meaning that the above print function might actually be a constructor for a print class object. The only way to really tell is by looking at the code in the function and how that function is used by the rest of the code.

For example:

function print(msg)

{

this.message = msg;

}

A function like this is a constructor for a print class object. The only real way we can tell is from the use of “this” in the code for the function. Remember that variables can be created on the fly in JavaScript, so we can create instance variables for our print class on the fly as well.

The other way to tell if print is meant to be a constructor, rather than just a run of the mill function is to see if there are any methods declared for the print class object. Such methods look like this:

print.prototype.store = function(msg)

{

this.message = msg;

}

This creates a method called store for use with the print class.

Utilizing an Object

To create an instance of an object in JavaScript is very similar to how you go about it in Java. For Example: var myPrint = new print(‘Hello’);

Would create an instance of the print class with the variable myPrint as a reference to the object.

We can then use the methods of our object very much like we would in Java as well:

myPrint.store(‘Good Bye’);

Calls the store method seen previously.

Cookies

Attached to each document object is the cookie property for that document. The cookie property contains all cookies for the document on that client’s web server. It is also the property we use to store a cookie.

Examples:

var allcookies = document.cookie;

-Stores all the cookies for the document in the variable allcookies.

document.cookie = “version=myCookie;expires=Fri, 02-Jan-1970 00:00:00 GMT”;

-Sets the cookie property for the document to a specific set of text.

Cookies have five attributes: value, expires, path, domain, and security. Not all of these attributes are required when creating a cookie. The general format for a cookie is such:

value ; expires = date ; path = path ; domain = domain ; secure

For Example:

document.cookie = “version = 3.1;expires = Fri, 02-Jan-1970 00:00:00 GMT;path=/lab;domain=clipper.ship.edu”;

Creates a cookie that stores the value 3.1 under the name version, along with an expiration date, path information, and domain information on where the cookie might be stored.

In Chapter 16 of the JavaScript book is code for a Cookie class, to make working with cookies in JavaScript easier. You can get this code off of my web site under the links for Labs and Assignments.

When looking at this code notice that even though the constructor for a Cookie object expects 6 parameters passed into it, that we don’t have to pass in all 6. This is JavaScript allowing us to create one overloaded constructor (This will work with functions as well).

Why might this be a bad thing? a good thing?

Notice in the example from Chapter 16 that we can continue to add instance variables at will to our instantiation of the Cookie class. This means that we could have two separate instantiations of the Cookie class with different instance variables. Try doing this now by adding the following code:

var visitordata2 = new Cookie(document, “test_guy”, 200);

visitordata2.grb = 7;

document.write('');

for(var prop in visitordata)

{

// We don’t want to list the methods of the object.

if ((typeof visitordata[prop]) != 'function')

{

document.write('Visitordata prop: '+prop+' ');

}

}

for(var prop in visitordata2)

{

// We don’t want to list the methods of the object.

if ((typeof visitordata2[prop]) != 'function')

{

document.write('Visitordata 2 prop: '+prop+' ');

}

}

The following shows how to add a property to an object dynamically in JavaScript:

this[“color”] = “Blue”;

visitordata[“color”] = ”Blue”;

This technique is used in the load method for the Cookie class.

See if you can’t use this to save the settings for Project 4, so that when a user connects back again, it is set to what the last selected. You’ll want to add instance variables to your Cookie object that store the needed information about each setting for each paragraph. Also, best to try getting it to work for just one of the paragraphs, rather than all four at once.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download