setting and calling a cookie in javascript

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

  1. cwesty macrumors member

    Joined:
    Oct 22, 2005
    #1
    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">

    setCookie();

    </script>

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

    any thoughts?
     
  2. mnkeybsness macrumors 68030

    mnkeybsness

    Joined:
    Jun 25, 2001
    Location:
    Moneyapolis, Minnesota
    #2
    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

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

    -T
     
  4. cwesty thread starter macrumors member

    Joined:
    Oct 22, 2005
    #4

    it's:


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

Share This Page