Closed Thread
Results 1 to 7 of 7

Thread: Creating Cookies

  1. #1
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Creating Cookies

    how can i create a cookie in java script? i basically want a function that does the equivilant of this in php

    Code:
    setcookie('clicked'"TRUE"time()+99999
    what i intend is to have a link, and when it is clicked, the javascript create a cookie

    HTML Code:
    <a href="http://www.google.com" onclick"CookieClick()">gooooogle</a>
    or something like that

    CookieClick() would then create the cookie

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Blaze's Avatar
    Blaze is offline Programmer
    Join Date
    Jun 2006
    Posts
    117
    Rep Power
    0
    You access them by the document.cookie

    Code:
    document.cookie =
      'ppkcookie1=testcookie; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/'
    1. First the name-value pair ('ppkcookie1=testcookie')
    2. then a semicolon and a space
    3. then the expiry date in the correct format ('expires=Thu, 2 Aug 2001 20:47:11 UTC')
    4. again a semicolon and a space
    5. then the path (path=/)


    From: http://www.quirksmode.org/js/cookies.html

  4. #3
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    that seems easy enough..also rather then specifying a specific date, would it be possable to create it so the cookies expire 24 hours after they are created (dynamically)

  5. #4
    Void's Avatar
    Void is offline Programming Expert
    Join Date
    Jun 2006
    Posts
    410
    Rep Power
    23
    Yes, here is some code:

    Code:
    var expireDate = new Date()
    expireDate.setHours(expireDate.getHours()-10)
    Add it here:

    Code:
    document.cookie =
      'ppkcookie1=testcookie; path=/; expires=' + expireDate.toGMTString();
    However, this is untested. I just took it from some other code on the web.
    Void

  6. #5
    john123 is offline Newbie
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Re: Creating Cookies

    Quote Originally Posted by Void View Post
    Yes, here is some code:

    Code:
    var expireDate = new Date()
    expireDate.setHours(expireDate.getHours()-10)
    Add it here:

    Code:
    document.cookie =
      'ppkcookie1=testcookie; path=/; expires=' + expireDate.toGMTString();
    However, this is untested. I just took it from some other code on the web.

    that is also fairly simple

  7. #6
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Re: Creating Cookies

    Use the cookie property of the document object. cookie is simply a single string that stores all of the individual cookies for that web page. Unlike other strings, it is mutable, so you can set its value as you would a variable.

    I suggest you read Javascript: The Definitive Guide to learn more about this. I got it from Amazon for about $25, read it, and now I'm pretty much an expert on Javascript (well, sort of. I still have a long way to go, but I'm pretty proficient). There's an entire chapter devoted to cookies. It's really good if you want to gain a full, in-depth understanding of the language.
    Last edited by Roger; 08-16-2010 at 09:57 PM.
    Life's too short to be cool. Be a nerd.

  8. #7
    UbuntuX's Avatar
    UbuntuX is offline Learning Programmer
    Join Date
    Mar 2010
    Posts
    45
    Rep Power
    0

    Re: Creating Cookies

    document.cookie ="CookieName=Value ; expires=Expire Valid Date ; path= ; Domain ; Secure"

    Date should be in UTC or GMT

    It's easy if u can't it means noob

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Using Cookies in PHP
    By Vswe in forum PHP Tutorials
    Replies: 13
    Last Post: 01-31-2010, 10:32 AM
  2. Cookies
    By PGP_Protector in forum C# Programming
    Replies: 0
    Last Post: 11-23-2009, 02:17 PM
  3. Cookies gone?
    By Jaan in forum PHP Development
    Replies: 2
    Last Post: 02-18-2009, 02:32 PM
  4. Cookies problemo
    By Jaan in forum PHP Development
    Replies: 2
    Last Post: 06-17-2007, 09:41 PM
  5. Cookies
    By dirkfirst in forum HTML Programming
    Replies: 10
    Last Post: 01-06-2007, 02:11 AM

Tags for this Thread

Bookmarks

Posting Permissions

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