Setting a Cookie using Javascript
To set cookies in Javascript use document.cookie
document.cookie="iltccook=got it;domain=ilovethecode.com;expires=Mon, 18-Feb-2036 05:43:38 GMT;path=/";
-You named your cookie
iltccook because you like the way it sounds.
-You give it a value of
got it because you think it's clever.
-When a user accesses your page that contains this script, they will get a cookie that lasts until 2036.
NOTES:
-The domain value must be your domain or subdomain (the domain where the script is).
-The time must be in this format Mon, 18-Feb-2036 05:43:38 GMT
-20 cookie per domain limit
You can give the cookie more than one value like so:
var huhu="got it,got more,and still more";
document.cookie="iltccook=" + huhu + ";domain=ilovethecode.com;expires=Mon, 18-Feb-2036 05:43:38 GMT;path=/";
NOTES:
-We use plus symbols to insert our variable
huhu into the cookie.
-Cookies with the same name overwrite the previous cookie.
To print the cookie back to the browser:
var ko=document.cookie;
document.write(ko);
or
document.write(document.cookie);
To see your all your cookies:
Mac Firefox - Preferences > Privacy > View Cookies > Then search for your domain
Mac Safari - Preferences > Advanced > Show Cookies
Windows Firefox - Tools > Options... > Privacy > Cookies > View Cookies
Windows IE - Tools > Internet Options... > General (Tab) > Settings... > View Files...
Free Copy and Paste Javascript Code (change domain to your domain; change name and value if you like):
<script type="text/javascript">
<!--
//set cookie number one - iltccook
document.cookie="iltccook=got it;domain=ilovethecode.com;expires=Mon, 18-Feb-2036 05:43:38 GMT;path=/";
//set cookie number two - iltccook2
var huhu="got it,got more,and still more";
document.cookie="iltccook2=" + huhu + ";domain=ilovethecode.com;expires=Mon, 18-Feb-2036 05:43:38 GMT;path=/";
//print cookies set by this domain
document.write(document.cookie);
-->
</script>
Resources:
Formatting Time in Javascript
Reading Cookies using Javascript
Search Google for "Javascript Set Cookie"
Microsoft - Specs for Date and Time Formatting
Web Monkey Cookie Tutorial
Spltting an Array in Javascript
Click HERE to comment or discuss at iLoveTheCode GOOGLE Group
| | | |
| | | |
| | | Dojo Matthew A. Russell... |