Jump to content

Javascript Cookies


autasebo

Recommended Posts

I want to create some Javascript code that will set a cookie the first time a user comes and displays "Welcome", and then displays "Welcome back. Your last visit was on" + date last at site when the user returns to the page. No pop-ups to ask the users name. Thanks!!!P.S. Anyone with working cookie code is greatly praised!!

Link to comment
Share on other sites

Where you able to follow along with the tutorial so that a cookie was being written to your computer? If so, you can store the current date as the value of the cookie rather than the "username" and call the cookie something like "LastVisit".The new "checkCookie" function could look like this:

function checkCookie(){	// we'll use displaytext to display the text to the screen.	var displaytext = "Welcome";  	// here we try to get the cookie.	lastvisit = getCookie("LastVisit");	if(lastvisit != null)	{  // we found the cookie.  Let's add some more text to our message		displaytext += " back.  Your last visit was on " + lastvisit;	}	else	{  // there wasn't a cookie, let's set one and assign the current date as the value.		setCookie("LastVisit", new Date(), 365)	}	// now we'll write the message to the screen.	document.write(displaytext);}

For more elaborate ways to get the text displayed to the screen, I would suggest looking at the HTML DOM Tutorial and learn about the "innerHTML" property.

Link to comment
Share on other sites

jesh pretty much nailed it on the head,if you need to you could set the cookie witht he unix timestamp of the time they visited and then use Js's DATE object to get the variables from there, but hey your choice.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...