15
JavaScript Part 9 George Mason University June 23, 2010

JavaScript Part 9 George Mason University June 23, 2010

Embed Size (px)

Citation preview

Page 1: JavaScript Part 9 George Mason University June 23, 2010

JavaScriptPart 9

George Mason University

June 23, 2010

Page 2: JavaScript Part 9 George Mason University June 23, 2010

Topics

• Cookies

• QueryString

• Menus

Page 3: JavaScript Part 9 George Mason University June 23, 2010

Cookies

• Cookies are a small amount of data stored by the web browser and associated with a particular web page or site

• Cookies are transient by default; the values go away when the user exits the browser

• If you want to have the cookie last longer, you need to specify the expiration date of the cookie

Page 4: JavaScript Part 9 George Mason University June 23, 2010

Cookie Duration

• The original way to specify cookie duration was by setting an expiration date in the future

• This still works, but now there is a max-age attribute which specifies the lifetime of the cookie in seconds

• Using either of these causes the browser to save the cookie in a file on the hard drive

Page 5: JavaScript Part 9 George Mason University June 23, 2010

Cookie Access

• By default, a cookie is accessible to the web page that created it, any other page in the same folder, or any subdirectories of that folder

• You can set the path attribute to allow access from other folders

Page 6: JavaScript Part 9 George Mason University June 23, 2010

Cookie Domain

• By default, cookies are only accessible to pages on the same server from which they were set

• Large web sites however may utilize several servers and the domain attribute can make the cookie available on multiple servers

• You cannot make the cookie available to another domain

Page 7: JavaScript Part 9 George Mason University June 23, 2010

Setting Cookies

• document.cookie = "cookieName"=cookieValue + ",expires=" + expirationDateGMT+ "; path=" + URLpath + "; domain"=siteDomain

Only the initial name/value pair is required; often the cookieValue will have been entered by the user in a form

-To change the value of a cookie, set its value again using the same name, path, and domain

Page 8: JavaScript Part 9 George Mason University June 23, 2010

Deleting Cookies

• To delete a cookie, specify a max-age attribute of 0 or use the expires attribute and specify an expiration date in the past

Page 9: JavaScript Part 9 George Mason University June 23, 2010

Reading Cookies

• document.cookie returns a String that contains all cookies that apply to the current document

• This is a set of name-value pairs, separated by semicolons and does not include any of the attributes that may have been set for the cookie

• You then parse this string using the String methods of indexOf(), substring(), and split()

Page 10: JavaScript Part 9 George Mason University June 23, 2010

Parsing Cookies

The split() method creates an array of Strings; for example, since the name value pairs are separated by semicolons; one can get the individual cookies by the following code:

allcookies = document.cookie;

cookieArray = allcookies.split(";");

Page 11: JavaScript Part 9 George Mason University June 23, 2010

Query String

• The query string is the portion of a URL following a question mark

• The query string consists of a series of name=value pairs, separated by &'s

• The query string can be formed by submitting a form using the GET method or by creating a link with the name/value pairs embedded in the link

Page 12: JavaScript Part 9 George Mason University June 23, 2010

Query Strings and JavaScript

• The query string of a page can be accessed by window.location.search – this includes the ?

• The query string can be parsed using the same String methods and techniques used in the parsing of cookies

• Server-side programming languages provide methods to obtain the value for a specified name in a query string without having to rely on String methods

Page 13: JavaScript Part 9 George Mason University June 23, 2010

Menus

• Although we've seen that menus can be done just with CSS, more commonly they are done with a combination of JavaScript and CSS

• CSS properties• visibility rather than display• positioning – absolute rather than relative• left and top• text-decoration

Page 14: JavaScript Part 9 George Mason University June 23, 2010

Menus (cont.)

• JavaScript• onmouseover and onmouseout event handlers

used• dynamically change CSS properties, primarily

visibility

Page 15: JavaScript Part 9 George Mason University June 23, 2010

Menus<style>

#m1, #m2 {position:absolute; height: 100px; width: 200px;}

#t1, #t2 {visibility:hidden; position:absolute; left:50px;}

</style></head>

<body><span id="m1" onmouseover="document.getElementById('t1').style.visibility='visible';

onmouseout="document.getElementById('t1').style.visibility='hidden';">

<a href="http://www.nba.com" >NBA</a><br />

<div id="t1"><a href="http://www.lalakers.com">Lakers</a><br />

<a href="http://bostonceltics.com">Celtics</a></div></span><br />

<span id="m2" onmouseover="document.getElementById('t2').style.visibility='visible';"

onmouseout="document.getElementById('t2').style.visibility='hidden';>

<a href="http://www.fcc.gov.com" >FCC</a><br />

<div id="t2"><a href="http://www.nbc.com">NBC</a><br />

<a href="http://www.cbs.com">CBS</a>

</div>

</span>

Content