# Does this cookie script look correct?



## stinger608 (Dec 8, 2010)

Just wondering if this script to include a JavaScript pop up look correct? Kind of new to all of this, so any advice would be much appreciated


```
<script type="text/javascript">
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function checkCookie()
{
username=getCookie('username');
if (username!=null && username!="")
  {
  alert('Welcome back, glad you stopped by '+username+'!');
  }
else
  {
  username=prompt('Please enter your name:',"");
  if (username!=null && username!="")
    {
    setCookie('username',username,365);
    }
  }
}
</script>
```


----------



## Disparia (Dec 11, 2010)

Javascript pop-up to set cookies? Since you're new to this, I'm going to let you off with a warning. But don't let me catch you in this part of the internet again.


----------



## Peter1986C (Dec 11, 2010)

As a person not knowledgeable about JavaScript, I am forced to read the valus of the string variables only, and it seems to me that what you want to happen is that once someone logs in, a cookie is placed (duh!) and a pop-up says welcome to that person.
However, a pop-up blocker will probably block the pop-up because many pop-ups are evil (at least on "evil" sites). And if my guess about your goal was correct, then why don't you integrate the welcome message in the web page itself, like this forum is doing? And more importantly, do you actually _need_ JS to place cookies? As a person that is not a web dev I again have to guess, but can't the cookie thing be done via html or whatever?


----------



## stinger608 (Dec 11, 2010)

Jizzler said:


> Javascript pop-up to set cookies? Since you're new to this, I'm going to let you off with a warning. But don't let me catch you in this part of the internet again.



Understood LOL



Chevalr1c said:


> As a person not knowledgeable about JavaScript, I am forced to read the valus of the string variables only, and it seems to me that what you want to happen is that once someone logs in, a cookie is placed (duh!) and a pop-up says welcome to that person.
> However, a pop-up blocker will probably block the pop-up because many pop-ups are evil (at least on "evil" sites). And if my guess about your goal was correct, then why don't you integrate the welcome message in the web page itself, like this forum is doing? And more importantly, do you actually _need_ JS to place cookies? As a person that is not a web dev I again have to guess, but can't the cookie thing be done via html or whatever?




The only thing this is, is a class project. Just making sure that it works is all. And your right Chev it is a pop-up that welcomes the person back again.


----------

