HTML Goodies: Script Tip: Week 64

By Joe Burns


Qwake Tip...

     So we know how to set and retrieve a cookie. Now let's start to play with it. In this case I want the message "Nice to see you" when the user uses the cookie for the first time. I then want the message "Welcome back" each time following. I'll show how here.

Try out the NEW Script

Note that if you've tried the script before,
it won't give you the prompt - it'll just say Welcome Back. If you can - go into your Windows directory and erase the cookie this set to see the full effect.

Here's the New Code

     When I set out to do this I knew the answer was going to be an if statement of some kind. It would say "if the user is a first-timer, use this greeting. If not - use this."

     The question then becomes what would you use to check off of? The answer is a new variable I named "FirstTime". The variable is stuck into the script in specific places so that if the user is getting a cookie for the first time, their "FirstTime" variable would be set to "y". All times past that, the variable would be set to "n". Then I could do a simple check if the variable was "y" or "n" and set the document.write as I saw fit.

     Notice the N and the Y are in quotes. That sets them aside as text strings rather than values.

Place the Variable

     Here's the top of the script. Look for the "FirstTime" variable:

cookie_name = "NameCookie2010";
var GuestName;
var FirstTime

function putCookie()
index = document.cookie.indexOf(cookie_name);
FirstTime = "n"


FirstTime = "y"
index = -1;


     Notice first off that I set "var FirstTime" so it would be recognized the rest of the way through the script. I often refer to this as "putting the variable into play".

     Strange as it may seem, the first thing you check is if a cookie exists, so the first setting "FirstTime" could receive is "n", meaning it isn't their first time. See the placement? It's just after the index of the cookie was found and set to a variable name.

     The reason for the placement is because after that point in the script, the majority of things are meant to set new cookies, right? If that's true, then somewhere in the new cookies I need to put the line that sets "FirstTime" to "y". Look above. You'll see it. It's in the first "else" function.

     I put it there because of the structure of the script. We wanted the placement so that when "FirstTime" is given a value, at no other time in the script would the value change. By putting it in the first If and else statements, we can be assured that if there is a cookie, "FirstTime" will be set to "n", and since the "else" is skipped if the original statement is true, then we know we can safely place the second "FirstTime" there.

     If there is a cookie, the script sets it to "n" and skips the else. If not, the else comes into play and sets "FirstTime" to "y". With me?

     OK, that's the hard part. Now the fun. This little piece of code posts a greeting depending on which value was assigned:

if (FirstTime == "y")
{document.write("Hello, "+GuestName+", nice to meet you!!!");}
{document.write("Hello, "+GuestName+", welcome back!!!");}

     It's another If statement that only tests if "FirstTime" is equal to "y". If it isn't, then we know it has to be "n" and the "else" statement will take it from there.

     You should be able to take it from here. If it's "y" then the greeting reads "nice to meet you". If it's "n" the greeting reads "welcome back".

     Use it good health.

Next Week: Color Script

     Do YOU have a Script Tip you'd like to share? How about suggesting a Script Tip to write about? I'd love to hear it. Write me at:

Learn to write your own JavaScripts with the HTML Goodies 30-Step Primer Series

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date