SitePoint Sponsor |
|
User Tag List
Results 1 to 2 of 2
Thread: Setting a Cookie
Threaded View
-
Feb 20, 2007, 15:52 #1
- Join Date
- Jun 2006
- Location
- London, UK
- Posts
- 154
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Setting a Cookie
I need to set a cookie using a sumbit button, i have the code and I just cant get it to run.
Could you take a quick look at it and let me know if theer is a glaring problem.
The button code:
Code:<input type = "submit" value = "Submit" onclick = "setCookie()" />
Code:window.onload = usercookieset; //calls function usercookieset to either create cookie or check the value function usercookieset(){ //sets the function var userName = "";//declares the variable with a null value if (document.cookie != ""){//tests the value of the cookie to see if its not set to null userName = document.cookie.split ("=")[1]; document.getElementById("username").value = userName;//retrieves the value of the username text field and stores it in the userName variable } function setCookie(){ //function that is called to run the above function var expireDate = new Date();//gets the date and stores it as a variable expireDate.setMonth(expireDate.getMonth()+12);//sets the expiry time of the cookie var userName = document.getElementById("username").value; document.cookie = "userName=" + userName + ";expires=" + expireDate.toGMTString(); }
Bookmarks