|
|
 |
RE: FN-FORUM frame jammin
date posted 1st May 2001 08:34
>>Oh by the way you wouldn't know how to get a input text box to
>>display info
>>from a cookie.
This is what your looing for ?
(sorry its lost the formating though ;-(
dec
Using Cookies: an Example
Using the cookie functions defined in the previous section, you can create a
simple page users can fill in to "register" when they visit your page. If
they return to your page within a year, they will see a personal greeting.
The following function returns a cookie value, given the name of the cookie:
function getCookie(Name) { var search = Name + "=" if
(document.cookie.length > 0) { // if there are any cookies offset =
document.cookie.indexOf(search) if (offset != -1) { // if cookie
exists offset += search.length // set index of beginning
of value end = document.cookie.indexOf(";", offset) // set
index of end of cookie value if (end == -1) end =
document.cookie.length return
unescape(document.cookie.substring(offset, end)) } }}
You need to define one additional function in the HEAD of the document. This
function, register, creates a cookie with the name TheCoolJavaScriptPage and
the value passed to it as an argument.
function register(name) { var today = new Date() var expires = new
Date() expires.setTime(today.getTime() + 1000*60*60*24*365)
setCookie("TheCoolJavaScriptPage", name, expires)}
The BODY of the document uses getCookie (defined in the previous section) to
check whether the cookie for TheCoolJavaScriptPage exists and displays a
greeting if it does. Then there is a form that calls register to add a
cookie. The onClick event handler also calls history.go(0) to redraw the
page.
Register Your Name with the Cookie-Meistervar
yourname = getCookie("TheCoolJavaScriptPage") if (yourname != null)
document.write("Welcome Back, ", yourname)else document.write("You
haven't been here in the last year...")
Enter your name. When you return to this page within a year, you will be
greeted with a personalized greeting. Enter your name:
|
 |
|