setting and calling a cookie in javascript

Discussion in 'Web Design and Development' started by cwesty, Apr 23, 2007.

  1. cwesty macrumors member

    Oct 22, 2005
    Hello all.

    I am trying to set a cooie and have it called once the page is opened...

    the function i've used is:

    function setCookie() {
    document.cookie = "pageVisit="+now+";expires=" + expireDate.toGMTString(value)

    which is stored in a seperate .js file.

    On the index page, there is the following embedded in the html:

    <script language="javascript" type="text/javascript">



    For some reason, no cookie is being set, and wasn't sure why...

    any thoughts?
  2. mnkeybsness macrumors 68030


    Jun 25, 2001
    Moneyapolis, Minnesota
    What is the variable value? are you setting that elsewhere? I don't think you actually need that there at all. Also, is the external javascript file loaded before you run the inline setCookie()?
  3. Trogloxene macrumors regular

    Apr 17, 2007
    Many browsers have an option where you can turn off the option to accept cookies. Easy to check...

  4. cwesty thread starter macrumors member

    Oct 22, 2005


    now = new Date
    expireDate = new Date
    lastVisit = new Date(cookieVal("pageVisit"))
    document.cookie = "pageVisit="+now+";expires=" + expireDate.toGMTString()

Share This Page