JavaScript Cookies: Making the Most from the Drudgery of Others | 7
|
|
JavaScript Cookies:
Making the Most from the Drudgery of Others
Now that we know what they are and how they work, let's insert them into an HTML document. You can write your own cookie scripts, but why not use some ready-to-eat cookie scripts created by the Pros themselves? They're usually free and you can modify them for your own use. We'll use Doc JavaScript's cookie scripts, and make some simple modifications. You can also find some good examples and code at JavaScriptSource.com. We'll also explore how to combine different scripts to make your own cookie recipes!
Cookie Examples - Seeing them in action
(Make sure that your browser is set to accept cookies for
this section.)
Examples will spawn a new window.
1. Basic Example - Original Doc JavaScript Column is Here:
(http://www.webreference.com/js/column8/)
Let's look at a basic example at a cookie in action. This example simply
uses a JavaScript prompt that asks the user for input - in this case it's
a name. The name is then stored in the cookie file on the user's computer.
When the user returns, the name is there giving them a personalized message.
Click here for
demo
2. Using A Counter - Original Doc JavaScript
Column is Here
(http://webreference.com/js/column8/counter.html)
This example keeps track of how many visits a user has made to a given URL. When
the user returns, the number increases by 1.
Click here for demo and code
3. Combining 2 Scripts
Now let's combine the first 2 cookie scripts to create a personal greeting.
Here we will modify and combine the 2 scripts.
Click here for demo and code
4. Getting creative with 3 scripts
This time we get creative. We'll combine the first 2 script examples, then
add a third. The catch here is that instead of displaying the visits using
the "document.write" method, we'll give the user the option of clicking on
a link to launch a pop up window to display the visit number. I used the Doc
JavaScript
handy dandy interactive form to "construct a script segment that
launches a window with specified features". This one is fun to experiment
with.
Click here
for demo and code
| To Continue, Use The Arrow Buttons |
|
Comments are
welcome
Produced by Greg Meckes
Created: Jan. 12, 2000
Revised: Jan. 14, 2000
URL: http://webreference.com/programming/javascript/cookies/page5.html

Find a programming school near you