Professional Documents
Culture Documents
This is an intense lesson where you can learn how to design menus that utilize
JavaScript. You will then learn all about the famous Cookies that you have heard
so much about. Here, I have tried to cover these two subjects "step by step" so
that you can understand the workings of both.
Menus
You can use the Rollovers that we talked about in Lesson 12 to create some
cool menus if you want. That is what I did for the menu on the left side of each of
these pages.
Lesson 13
The JavaScript code for a menu such as the one above is really quite short but I
find it difficult to understand. So, I decided to take the lengthy approach to
explaining the code here. If you are interested in just the final code, then feel free
to go here to see it.
Here is the HTML code I used to create the drop down list for our demo:
<FORM NAME="demo">
<SELECT NAME="menu" onChange="loadPage()">
<OPTION VALUE="lesson11.htm">Lesson 11
<OPTION VALUE="lesson12.htm">Lesson 12
<OPTION SELECTED VALUE="lesson13.htm">Lesson 13
<OPTION VALUE="lesson14.htm">Lesson 14
</SELECT>
</FORM>
As shown in the above HTML code, the <SELECT> tag is used to create a drop
down list.
Nested between the <SELECT> and </SELECT> tags are <OPTION> tags.
Note that there is one <OPTION> tag for each menu entry. The V A LU E property
of the <OPTION> tag contains the URL of a page and the text following the tag
javascriptmall.com/learn/lesson13.htm 1/7
10/11/2009 Learning JavaScript - Menus and Cooki…
appears in the drop down menu list.
Note that our third <OPTION> tag in the list has S E LE CT E D in it. This identifies
the selection that will be the default when the page loads.
The onChange method of the <SELECT> tag calls a function that is used to load
the specified URL. Here is the code for that function.
function loadPage(){
var sel_idx = document.demo.menu.selectedIndex
var urlToLoad = document.demo.menu.options[sel_idx].value
location.href = urlToLoad
}
Click here if you would like to test our drop down list menu and see the values of
s e l_id x and urlT o Lo a d that result when you select each of the possible
options.
The last line of the function loads the page into the browser utilizing the lo c a tio n
o b je c t.
t
function loadPage(fInfo){
location.href = fInfo.options[fInfo.selectedIndex].value
}
<FORM NAME="demo">
<SELECT NAME="menu" onChange="loadPage(this)">
<OPTION VALUE="lesson11.htm">Lesson 11
<OPTION VALUE="lesson12.htm">Lesson 12
<OPTION SELECTED VALUE="lesson13.htm">Lesson 13
<OPTION VALUE="lesson14.htm">Lesson 14
</SELECT>
</FORM>
javascriptmall.com/learn/lesson13.htm 2/7
10/11/2009 Learning JavaScript - Menus and Cooki…
original version of the lo a d Pa g e () function is now written on one line. If you
study it close, I think you will see that both versions will have the same results.
In our example menu, we passed the this keyword to a function from the
SE LE CT object. Therefore, in the function the variable fInfo refers to the
SE LE CT object or more specifically will be the same as using
document.demo.menu.
The lo c a tio n o b je c t is used to load a specific URL into your browser. Just
make location.href equal to the URL that you want to load. In our menu script
above I used a relative URL since the page was on my website. You can
specify the complete URL if you want. For instance, I could have used
location.href = "http://www.crays.com/learn/lesson11.htm"
to load my lesson 11 page. You will need to specify the complete URL for pages
that are not on your website.
The lo c a tio n o b je c t can be used to load pages into a different Frame if your
site is using Frames or into a different browser window. Visit our Library to learn
how to do this if you are interested.
By default the <SELECT> tag results in a drop down box like we demonstrated
above. Using the S IZE property, you can make the <SELECT> tag result in a
scrolling list. For example
<SELECT SIZE=3 NAME="menuDemo" onChange="loadPage(this)">
will result in a scrolling list of 3 elements.
Cookies
A Cookie is a small piece of data that is stored in memory or on your hard drive.
These cookies are used to give your web browser memory to be used from one
web page to the other or from one visit to the other.
Many people are leery of Cookies because of a fear that their privacy is being
invaded. In the case of JavaScript, all cookie data remains on the computer that
created it. You need a more sophisticated language if you want to send this data
back to the server.
If you look back at the Property Summary of the document Object in lesson 12,
you will see that a cookie is a string value. This string contains information in a
special format that we can read and write to the hard drive.
javascriptmall.com/learn/lesson13.htm 3/7
10/11/2009 Learning JavaScript - Menus and Cooki…
Cookies are stored in a file named cookies.txt if you are using Netscape
Navigator on a PC and in a file named MagicCookie on the Mac. Internet
Explorer saves each individual cookie as a file in a directory named Cookies. I
recommend that you make a second copy and use that if you want to look at a
cookie file in a editor since any changes will probably make the file useless.
First, press the Ge t Co o k ie S tring button. An alert box is displayed with nothing
in it.
Enter something into the te xt b o x and press Se t Co o k ie . Now when you press
the Ge t Co o k ie S tring button, the alert box displays
no1=text you entered (no1 is the name that I gave the cookie).
Here is the syntax that you use to set this simple cookie.
document.cookie="nameofCookie=cookieData"
For our form, I named the cookie no1 and got the cookieData from our te xt
b o x in the form. Here is the code that I used. You will see how this all worked by
examining the onClick events for the two buttons.
<FORM name="frm1">
<INPUT type="text" length=20 name="txt1">
<P>
<INPUT type="button" value="Set Cookie" name="set1"
onClick='document.cookie="no1="+document.frm1.txt1.value'>
<INPUT type="button" value="Get Cookie String" name="get1"
onClick='alert(document.cookie)'>
</FORM>
Realize that most of the code shown above was used for this demo and does
not represent a practical way to set or display a cookie. The code that was used
to set the cookie was made bold so that it can easily be identified. The cookie
data was shown by displaying it in a simple alert box using the onClick event of
the second button.
A Second Cookie
Here is a form that will make a second cookie for us named no 2.
2
javascriptmall.com/learn/lesson13.htm 4/7
10/11/2009 Learning JavaScript - Menus and Cooki…
When you enter something into the te xt b o x,x press Se t Co o k ie and then press
Ge t Co o k ie String , the alert box displays a string that contains the name and
data for both cookies. Note that the two cookies are separated by a semicolon
(; ).
<FORM name="frm2">
<INPUT type="text" length=20 name="txt2">
<P>
<INPUT type="button" value="Set Cookie" name="set2"
onClick='document.cookie="no2="+document.frm2.txt2.value'>
<INPUT type="button" value="Get Cookie String" name="get2"
onClick='alert(document.cookie)'>
</FORM>
document.cookie="nameofCookie=cookieData; expires=time"
Here are the functions that I used to make this third cookie.
function getCookieExpireDate(noDays){
var today = new Date()
var expr = new Date(today.getTime()+noDays*24*60*60*1000)
return expr.toGMTString()
}
function makeCookie(name, data, noDays){
var cookieStr = name + "="+ data
if (makeCookie.arguments.length > 2){
cookieStr += "; expires=" + getCookieExpireDate(noDays)
}
document.cookie = cookieStr
}
Let's examine the makeCookie() function. This function can be used to make a
temporary cookie like our first two or it can be used to make a cookie that will
javascriptmall.com/learn/lesson13.htm 5/7
10/11/2009 Learning JavaScript - Menus and Cooki…
persist on a hard drive for a given number of days. This works because
JavaScript functions can have a variable number of arguments.
I used 1 day as the time that the cookie will remain on your hard drive in our
example above. So you might want to confirm this by checking later on today or
tomorrow. Remember that cookies remain in memory until you close your
browser.
I think that you will find that the above function and the one in the next paragraph
will work for most simple cookie situations that you will be doing.
There might be a time when you want to delete a cookie from a hard drive. In that
case just set the expires argument to a value prior to the current date. Calling the
makeCookie() function with a negative argument for the number of days should
work just fine.
function readCookie(cookieName){
var searchName = cookieName + "="
var cookies = document.cookie
var start = cookies.indexOf(cookieName)
if (start == -1){ // cookie not found
return ""
}
start += searchName.length //start of the cookie data
var end = cookies.indexOf(";", start)
if (end == -1){
end = cookies.length
}
return cookies.substring(start, end)
}
You pass it the name of a cookie and it returns the data that is contained in the
cookie. Hopefully, by now you can look at the function and analyze what is
happening for yourself. Therefore, I don't plan on discussing it any further here.
You can prove that this function does indeed work by clicking on each of the
buttons below to extract the data from each cookie you created above.
javascriptmall.com/learn/lesson13.htm 6/7
10/11/2009 Learning JavaScript - Menus and Cooki…
Note that only the name and the data (name-data pair) is available to you using
JavaScript. Other data such as the expiration date is not available.
We have learned about two of the cookie attributes, the required name attribute
and the optional expires attribute.
By default, a cookie is assessable to the web page that created it and any other
web pages that are contained in its same directory including the sub directories.
The path and domain attributes allow you to specify additional directories and
servers that can access your cookies.
The last three attributes are beyond the scope of this class. If you want to know
more about them, then look in our library or read the technical details of cookies
from netscape
Assignment
1. Develop a simple website that contains a few pages. Use a drop down
list menu to navigate between the pages.
Solution
javascriptmall.com/learn/lesson13.htm 7/7