www.webdeveloper.com
Results 1 to 4 of 4

Thread: JS cookies

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Posts
    2

    JS cookies

    I am working on an exercise in javascript where you have can choose between four different css-files. So far so good.

    My problem is that I need to create a cookie that remember which css-file you choose if you have been at this site before. I have tried to solve this with document.cookie = "filename.css". This works perfect at my computer, if I put it out to my server it doesn't work at all. Could someone please help me?

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    First of all, cookies work with NVP (Name Value Pair); all you are providing is the V, without the N. ("filename.css"). You need to do document.cookie = "thisFile = filename.css;";

    Secondly, without an expire date the cookie is not planted on the user's hard drive; it's a session cookie that disappears as soon as you close the browser.

    This website has some good information on working with cookies in JavaScript http://www.quirksmode.org/js/cookies.html

  3. #3
    Join Date
    Jun 2011
    Posts
    2
    Thanks for your fast reply and help!

    could something like this work?
    document.cookie = '"thisFile = filename.css;"; expires=Thu, 4 Aug 2011 20:47:11 UTC; path=/'

  4. #4
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Quote Originally Posted by sunny_days View Post
    Thanks for your fast reply and help!

    could something like this work?
    document.cookie = '"thisFile = filename.css;"; expires=Thu, 4 Aug 2011 20:47:11 UTC; path=/'
    Almost.
    Code:
    document.cookie = 'thisFile = filename.css; expires=Thu, 4 Aug 2011 20:47:11 UTC;'
    You don't put quotes around the variable name or value. Path is really unnecessary, except for real detailed stuff.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles