Javascript - checking if cookie exists on page load isn't working for me -
i have follwing code :
<script type="text/javascript"> function myfuncc () { if (document.cookie.indexof("visited") >= 0) { // they've been here before. alert("hello again"); } else { // set new cookie expiry = new date(); expiry.settime(date.gettime()+(10*60*1000)); // ten minutes // date()'s togmtsting() method format date correctly cookie document.cookie = "visited=yes; expires=" + expiry.togmtstring(); alert("this first time"); } } </script> <script type="text/javascript"> window.onload = myfuncc; </script> as can see window.onload function, trying check if vistor had been @ site once page loads. using cookies so. problem is, can't see messages supposed to. knows why?
you have set expire date using toutcstring() method. have date object not initialized, try this:
function myfuncc () { if (document.cookie.indexof("visited") >= 0) { // they've been here before. alert("hello again"); } else { var expiry = new date(); expiry.settime(expiry.gettime()+(10*60*1000)); // ten minutes document.cookie = "visited=yes; expires=" + expiry.toutcstring(); alert("this first time"); } }
Comments
Post a Comment