Check for New Visitor in Javascript
<p>Im trying to find a clean way too see if a user of my website has visited recently. I need this done in javascript (due to caching), and i have come up with the code below. Is there a cleaner way to do this, ie. less lines of code :)</p>

<pre><code>function createCookie(name,value,days) {
    if (days) 
    {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i &lt; ca.length;i++) {
       var c = ca[i];
       while (c.charAt(0)==' ') c = c.substring(1,c.length);
       if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

var newuser = readCookie('newuser');

if (newuser == '1') {
    [do something with x]
}

else {
 createCookie('newuser','1',365);
}
</code></pre>
