Electric Type

Multimedia

About Us

News

Help

Advanced JavaScript Tutorial
Lesson 2

by Thau!

Page 13 — Cookie Path and Domain

This is the last tricky cookies hurdle: By default, a cookie can be read only by HTML pages that sit on the same Web server and in the same directory as the page that set the cookie.

For example, if you have a JavaScript on "http://chimp.webmonkey.com/food/bananas/banana_puree.htm" that asks people for their names, you might want to access a given name on another one of your Web pages, such as the homepage (http://chimp.webmonkey.com/.) To allow this, you have to set the "path" of the cookie. The "path" sets the top level directory from which a cookie can be read. Set the path of a cookie to the top-level directory of your Web pages, and the cookie is readable by all your Web pages.

Do this by adding path=/; to your cookie. If you just wanted the cookie readable solely in the "food" directory, you'd add path=/food; to your cookie.

A second hitch is that some Web sites have lots of little domains. For example, We might have pages at "chimp.website.com," "gorilla.website.com," and "ape.website.com." By default, if a Web page on "chimp.website.com" sets a cookie, only pages on "chimp.webmonkey.com" can read it. If we wanted all the machines in the "webmonkey.com" domain to read the cookie, we'd have to add "domain=webmonkey.com" to the cookie. Don't get clever though. Pages at "republicans.com" can't set or read cookies from "democrats.com."

To put all the above together — set a cookie on the Web page "http://chimp.webmonkey.com/food/bananas/banana_puree.htm" that we want to be readable by all pages — we'd have to do this:

function setCookie()

{

    var the_name = prompt("What's your name?","");

    var the_cookie = "cookie_puss=" + escape(the_name) + ";" ;

    var the_cookie = the_cookie + "path=/;";

    var the_cookie = the_cookie + "domain=webmonkey.com;";

    document.cookie = the_cookie;

}

And so ends the cookies lesson. Let's review what we've learned so far and then go onto a final exercise.

next page»


Tutorials  

User Blogs  

Teaching Tools  

Authoring  

Design  

Programming help  

Advanced Flash  

Javascript  

Glossary  

PHP Coding  

User Blogs

Screen Shots

Latest Updates

Contact Us

Valid HTML 4.01!
Valid CSS!

Breadcrumb

© ElectricType
Maintained by My-Hosts.com
Site map | Copyright | Disclaimer
Privacy policy | Acceptable Use Policy
Legal information.