• home
  • forum
  • my
  • kt
  • download
  • Cookies with Javascript

    Author: 2009-03-05 08:41:31 From:

    It is very common for a website to use cookies. Cookies are a small snippet of data that is stored on the client’s machine. Cookies can store anything from user information to session data.


    Cookies are needed because the HTTP protocol that is used around the web is state less. Web servers cannot remember information and require cookies to help sites remember who you are. You can see examples of this when you login too many sites. The websites use cookies to remember you.


    Let’s take a look at how to set, retrieve, display cookie value and then delete the cookie in javascript.


    Setting Cookies with Javascript


    We are going to create a javascript function called set_cookie() to set our cookie when the user visits a page.




    First, we set up the set_cookie() function. In order for this function to work properly, we need to pass in the name of the cookie, value of the cookie, and the date the cookie expires. Next, we make sure that we have the date the cookie expires. If we don’t have a expiration date then we assign a new one. Then, we use the document.cookie property to set the cookie. Document.cookie is a string property that allows you to read, create, modify, and delete cookies.


    Retrieving Cookies with Javascript




    Next, we create a function called get_cookie(). This function will retrieve he cookie that we set above. With this function, we must pass in the cookie name (cookie_name). Next, with in the function, we will use a regular expression to find the cookie name and the value that is associated with the name. If we find the cookie, we return the value within the cookie. If we don’t find that cookie, we return null.


    Deleting Cookies with Javascript




    To delete the cookie, we call the delete_cookie function. To delete the cookie, we simply call the set_cookie function. We set the value to “” and set the date to a previous date so the cookie is expired.


    Calling The Function


    Next, we will need to add a bit of javascript code to setup the expire date and call the functions.




    In the above code, we set the expiration date to expire 1 day from now. Then we call the set_cookies function where we pass in the name (“mycookie”), value (“value”) and expire date (expiredate). After we set the cookie, we retrieve the cookie with the get_cookie () function. We assign variable cookie_value the value that we return from the get_cookie() function. After we retrieve the cookie, we create a simple alert where we display the value in the cookie. Then we call the delete_cookie function to delete the cookie.

    discuss this topic to forum

    relation tutorial

    No information

    Category

      AJAX (63)
      Content Management (12)
      Cookies (6)
      Date and Time (16)
      Development (16)
      DHTML (21)
      Forms (8)
      Frequently Asked Questions (3)
      Image Display (13)
      Introduction to Javascript (9)
      Links and Buttons (7)
      Menus (2)
      Miscellaneous (12)
      Mouse Tricks (3)
      Navigation (11)
      Randomizing (6)
      Security (5)
      Text Effects (9)
      User Authentication (2)
      User Information (5)
      Windows and Frames (6)

    New

    Hot