how can i create a cookie in java script? i basically want a function that does the equivilant of this in php
what i intend is to have a link, and when it is clicked, the javascript create a cookieCode:setcookie('clicked', "TRUE", time()+99999)
or something like thatHTML Code:<a href="http://www.google.com" onclick"CookieClick()">gooooogle</a>
CookieClick() would then create the cookie
You access them by the document.cookie
1. First the name-value pair ('ppkcookie1=testcookie')Code:document.cookie = 'ppkcookie1=testcookie; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/'
2. then a semicolon and a space
3. then the expiry date in the correct format ('expires=Thu, 2 Aug 2001 20:47:11 UTC')
4. again a semicolon and a space
5. then the path (path=/)
From: http://www.quirksmode.org/js/cookies.html
that seems easy enough..also rather then specifying a specific date, would it be possable to create it so the cookies expire 24 hours after they are created (dynamically)
Yes, here is some code:
Add it here:Code:var expireDate = new Date() expireDate.setHours(expireDate.getHours()-10)
However, this is untested. I just took it from some other code on the web.Code:document.cookie = 'ppkcookie1=testcookie; path=/; expires=' + expireDate.toGMTString();
Void
Use the cookie property of the document object. cookie is simply a single string that stores all of the individual cookies for that web page. Unlike other strings, it is mutable, so you can set its value as you would a variable.
I suggest you read Javascript: The Definitive Guide to learn more about this. I got it from Amazon for about $25, read it, and now I'm pretty much an expert on Javascript (well, sort of. I still have a long way to go, but I'm pretty proficient). There's an entire chapter devoted to cookies. It's really good if you want to gain a full, in-depth understanding of the language.
Last edited by Roger; 08-16-2010 at 09:57 PM.
Life's too short to be cool. Be a nerd.
document.cookie ="CookieName=Value ; expires=Expire Valid Date ; path= ; Domain ; Secure"
Date should be in UTC or GMT
It's easy if u can't it means noob![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks