iLTC Home
Bookmark iLTC!

Categories

Perl
  • Perl Tutorials Beginners

  • Javascript
  • Javascript Tutorials
  • Javascript Tutorials Beginners

  • HTML
  • HTML Tutorials

  • Command Line
  • Command Line Tutorials

  • How Tos
  • Other Tips and Tricks



  • <script type='text/javascript'>
    2210818

    iLoveTheCode.com

    Reading Cookies using Javascript

    To read cookies with Javascript:

    var ko = document.cookie;

    document.write(ko);

    or

    document.write(document.cookie);


    -We named a variable ko because it's easy to type.
    -We stored the cookies in ko
    -We printed all cookies set by our domain to the browser, assuming there are some.
    -The second document.write is just a shortcut.


    Now, let's set two cookies first:

    document.cookie="iltccook=got it ONE;domain=ilovethecode.com;expires=Mon, 18-Feb-2036 05:43:38 GMT;path=/";
    document.cookie="iltccook2=got it Two;domain=ilovethecode.com;expires=Mon, 18-Feb-2036 05:43:38 GMT;path=/";

    Then we'll read and split them into usable pieces.

    var ko=document.cookie.split("; ");

    -We split the cookies into pieces by the semicolon and space. (semicolon-space is how all browsers store multiple cookies)
    -Now we'll print out their pieces.

    document.write(document.cookie + "<br>");
    document.write(ko[0] + "<br>");
    document.write(ko[1] + "<br>");

    -The first document.write prints all cookies from our domain.
    -The second document.write prints the cookie at index zero (aka the first cookie).
    -The third document.write prints the cookie at index one (aka the second cookie).


    You could split these down further still like so:

    var ji=ko[0].split("=");

    -ko[0] has our first cookie.
    -We split ko[0] by the equals sign and store the pieces in the variable we name ji
    -Now we print the pieces with four dashes in between to make it look pretty.

    document.write(ji[0] + " ---- " + ji[1]);



    Free Copy and Paste Javascript Code:

    <script type="text/javascript">
    <!--


    document.cookie="iltccook=got it ONE;domain=ilovethecode.com;expires=Mon, 18-Feb-2036 05:43:38 GMT;path=/";
    document.cookie="iltccook2=got it Two;domain=ilovethecode.com;expires=Mon, 18-Feb-2036 05:43:38 GMT;path=/";


    var ko=document.cookie.split("; ");

    document.write(document.cookie + "<br>");
    document.write(ko[0] + "<br>");
    document.write(ko[1] + "<br>");

    var ji=ko[0].split("=");

    document.write(ji[0] + " ---- " + ji[1]);

    -->
    </script>


    Resources:
    Setting a Cookie
    Spltting an Array in Javascript





    Previous Article: Setting a Cookie using Javascript
    Next Article: Getting Epoch Time in Javascript






    Search iLTC w/ Google




    Recent Articles

  • HTML Font Code
  • Javascript and CSS - Easy Pop Up Help Balloons
  • Use OnMouseOver in Javascript
  • Use onMouseOut in Javascript
  • Use innerHTML to Change Text on a Page
  • Renaming a File with Perl's RENAME Function
  • PRINT to Browser with document.write Command in Javascript
  • Passing an Argument or Variable to a Javascript
  • Opening an Alert Box in Javascript
  • Opening a REMOTE WINDOW in Javascript
  • OPEN and WRITE a Data File in Perl
  • OPEN and READ a Data File in Perl
  • Mimic Typing from a Textfield in a Textarea with Javascript
  • Javascript Reset Form








  • Perl Programming - Javascript Programming - HTML Code - Mac Unix Command Line - Maybe Some PHP - Free Scripts - ILoveTheCode
    All Content © 2006-2009 iLoveTheCode.com


    Contact Chris | About this Site


    2210030
    Counter by iLoveTheCode.com


    Last Modified: Saturday, 10-Jan-2009 05:40:24 EST



    Hosted as well :)
    Home-brewed logo - 1997