Jump to content

Trying to Set a Cookie


ScottyBoy

Recommended Posts

OK, here's what I'm trying to do...My ultimate goal here is to change CSS styles for specific users on one of my websites, but only if they can guess the secret password (yes, they get a hint). So, I wrote a script in hopes that it would check this value of a text field to see if the password is correct (this part works). Then, I want it to give a cookie to that user so that the browser remembers that they got the password right. Unfortunately I can't seem to get a cookie set. :) So I'm posting my code here, because you guys know what you're doing. :)here's the JS in the head section...

<script type="text/javascript" language="JavaScript"> // Set the variables to be used in the scriptvar now = new Date();var get = getCookie("postD");// I'm not sure what this does, but the tutorial says I need it *shrug*fixDate(now); // Break out the magnifying glass function check(password) {	 // I the password is right, gimme a cookieif (password.text.value == "secret"){	now.setTime(now.getTime() + 7 * 24 * 60 * 60 * 1000);   	// If I don't have a cookie, give me one	if (!get)	{		setCookie("postD", "Here I am", now);	}		// If I already got a cookie, I don't get another one. 	if (check)	{		alert('You already ate your cookie :S');	}}	 // If the password is wrong, let me knowelse{	alert('I\'m sorry, you got the password wrong :S');}	  // End the function } </script>

And here's the HTML form...

 <div align="center"> <form name="password" onSubmit="return check(this)"> <input type="text" name="text" value="" /> <input type="submit" value="Submit" /> </form> </div>

Link to comment
Share on other sites

Do you actually have getCookie and setCookie functions? Tutorials sometimes give the impression that these are built-in functions. They are not. Same with fixDate, whatever that is . . .

Link to comment
Share on other sites

Do you actually have getCookie and setCookie functions? Tutorials sometimes give the impression that these are built-in functions. They are not. Same with fixDate, whatever that is . . .
No I don't have those functions, that would explain why this doesn't work. What do I need to add to my code?
Link to comment
Share on other sites

I already looked at that tutorial, but I'm still a bit confused, so I'm going to bye a JavaScript book this week. Thanks for your help so far,Scotty :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...