#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    68
    Rep Power
    13

    Cookies and Javascript.


    I'm trying to figure how javascript handles cookies. Here are my questions:


    - Is it possible for a javascript to read a cookie that was set trough php?

    - How do you set a cookie in javascipt? Is there an aquivalent of the php function setcookie() ?

    How do you read cookies with javascript? I know in php you just type a variable wich is the name of the cookie, is it the same in JS?


    Finaly, if someone know a good, complete tutorial on cookies with JS, please tell me!

    Thanks
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    24
    Yes, javascript and php and asp and any cookies are interchangable, readable and settable by every language.

    Javascript:

    document.cookie = "cookiename=cookievalue"

    In javascript the retrieving of cookies is a bit difficult. What you actually get returned is a string representing every cookie. So to extract cookie info you need to substring it as such:

    var cookieVal = document.cookie.substring(document.cookie.indexOf("cookiename") + 11, document.cookie.length)

    Yup, it's crap, so you're better off using PHP/ASP to extract the cookie info.

IMN logo majestic logo threadwatch logo seochat tools logo