Semester 2 Lecture 2

advertisement
<b><u>Semester 2 Lecture 2</u></b><br />
This will be the last lecture which we will cover JavaScript, from now on we will be looking at JQuery.
<br /><br />
<b><u>Cookies: Maintaining State</u></b><br />
<b>What are Cookies</b><br /><br />
Cookies are very important, small pieces of information which are then stored on the hard drive, an
example would be the username and password or a URL, an example of a message that a cookie
would ask is something like, “Do you want to save your password for this site?”. When using cookies
I need to be aware and consider the ethical issues, privacy and secure issues. You can create up to
20 cookies, however it is unlikely that I would ever use that amount on a website. <br /><br />
We looked at the Shopping Cart example which allowed me to see what exactly the cookies stored,
this was good to see as it made it clear to me how beneficial storing information is to users eg
storing the number of items they want and when they go to the summary part of the payment,
when they return to the page the number they selected is still there. <br /><br />
<b>Cookie Object</b><br />
<b>Value:</b> document.cookie=”Username(this is the object)=grace(and this is the value)” <br />
<br />
<b>Expiration Date:</b> document.cookie = “username=grace; expires=Wed, 16-Jul-15 00:00:01
GMT” <br />
<br />
<b>Path domain:</b> document.cookie = “username=grace; expires=Wed, 16-Jul-15 00:00:01 GMT;
path=/aw; domain=amazon.com” <br />
<br />
<b>Security:</b> document.cookie = “username=john; expires=Wed, 16-Jul-15 00:00:01 GMT;
path=/aw; domain=amazon.com; secure” <br />
<br />
<b>Creating Cookies</b><br />
There are several ways of creating a cookie, they are as follows:<br /><br />
<li>document.cookie = “username=john”</li>
<li>document.cookie = “username=john; ¬
expires=Mon, 18-Jun-01 00:00:01 GMT”</li>
<li>document.cookie = “username=john; ¬
expires=Mon, 18-Jun-01 00:00:01 GMT; ¬
path=/aw; domain=stevenestrella.com</li>
<li>document.cookie = “username=john; ¬
expires=Mon, 18-Jun-01 00:00:01 GMT; ¬
path=/aw; domain=stevenestrella.com; secure”</li>
<br /><br />
<b>Creating and deleting cookies</b><br /><br />
When a JavaScript statement have cookies set in it when the web page is ran, the page assigns
content to the cookies which are in place. The content will include information about the domain
and directory location of the page that it created. To note: the escape function is to remove a space
which has been inserted either before or after a word eg “ Ellie”.
<br /><br />
<b>Setting Cookies</b><br />
function SetCookie(name,value,expires,path,domain,secure)[ <br />
var temp = name + "=" + escape(value); <br />
if (expires)[ <br />
temp += "; expires=" + expires.toGMTString();<br />
] <br />
if (path)[ <br />
temp += "; path=" + path; <br />
] <br />
if (domain)[ <br />
temp += "; domain=" + domain; <br />
] <br />
if (secure)[ <br />
temp += "; secure";<br />
] <br />
document.cookie = temp; <br />
] <br />
<br />
<b>Get Cookie</b><br /><br />
function GetCookie(name)[ <br />
var arg = name + "=";<br />
var alen = arg.length; <br />
var clen = document.cookie.length; <br />
var i = 0; <br />
while (i < clen) {<br />
var j = i + alen; <br />
if (document.cookie.substring(i,j) == arg)[ <br />
return getCookieVal(j); <br />
] <br />
i = document.cookie.indexOf(" ", i) + 1; <br />
if (i == 0) break; <br />
] <br />
return null; <br />
] <br />
<br />
<b>Get Cookie value</b><br />
function getCookieVal(offset)[ <br />
var endstr = document.cookie.indexOf(";", offset); <br />
if (endstr == -1)[ <br />
endstr = document.cookie.length; <br />
] <br />
return unescape(document.cookie.substring(offset,endstr)); <br />
] <br />
<br />
<b>Deleting a cookie</b><br />
function DeleteCookie (name,path,domain) [<br />
if (GetCookie(name)) [<br />
var temp = name + "=";<br />
temp += ((path) ? "; path=" + path : "");<br />
temp += ((domain) ? "; domain=" + domain : "");<br />
temp += "; expires=Thu, 01-Jan-70 00:00:01 GMT";<br />
document.cookie = temp; <br />
] <br />
] <br />
<br />
<b>Storing Preferences</b><br />
Cookies are commonly used for storing preferences which the user has made, an example being
login details, background colour. When the webpage retrieves the cookie, it gathers the information
and then acts on it, eg changing the background colour. <br /><br />
Download