views:

58

answers:

2

Hey All,

I've been out of web development for a while now so very rusty. I need to make a cookie

  1. If the user visits the site > set cookie
  2. If user comes back to site > read cookie
  3. If time since user last arrived is less than 24hrs > set the ID of a div to style="display:none;"

im struggling and would really appreciate some guidance.

thanks so much

A: 

With JavaScript you can
- read cookie:

var coo = [],
    a;
if(document.cookie != ''){
    $.each(document.cookie.split('; '), function(i, val){
        a = val.split('=');
        coo[a[0]] = a[1];
    });
}

here we have a coo with all cookies (coo['Cookie1'] == 'value').
- set cookie:

document.cookie = 'Cookie_1='+'value for this cookie';

BTW code is using jQuery for $.each.

NilColor
A: 

Heres the messy solution i cam up with in case anyone wanted to know:

function nameDefined(ckie,nme)

{ var splitValues var i for (i=0;i tvalue=getCookieValue(nvpair,cname) //Gets the value of the cookie if (tvalue == cvalue) return true else return false } else return false } function redirectLink() { if (testCookie("here10", "yes")) { //window.location="here.html" //Go to the location indicating the user has been here //alert("there"); window.document.getElementById("indicator").style.display = "none"; } else{ //alert(" not there"); var futdate = new Date() //Get the current time and date var expdate = futdate.getTime() //Get the milliseconds since Jan 1, 1970 expdate += 10000 //expires in 1 hour(milliseconds) futdate.setTime(expdate) var newCookie="here10=yes; path=/;" //Set the new cookie values up newCookie += " expires=" + futdate.toGMTString() window.document.cookie=newCookie //Write the cookie // window.location="not.html" //Go to the location indicating the user has not been here

  window.document.getElementById("indicator").style.display = "block";

} }

chris