iLTC Home
Bookmark iLTC!
iLTC Google Boards

Google Groups
Receive The Code in Email!



Categories

Perl
  • Perl Tutorials Beginners

    Javascript
  • Javascript Tutorials
  • Javascript Tutorials Beginners

    HTML
  • HTML Tutorials



  • Perl with cheese...
    143,040

    iLoveTheCode.com
    Javascript > Javascript Tutorials How To Easy > Reading Cookies using Javascript

    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



    Click HERE to comment or discuss at iLoveTheCode GOOGLE Group
    Previous Article: Setting a Cookie using Javascript
    Next Article: Getting Epoch Time in Javascript





    HTML & XHTMLHTML & XHTML

    Chuck Musciano, Bi...

    New $29.99

    CSSCSS

    David McFarland

    New $23.09

    JavaScriptJavaScript

    David Flanagan

    New $31.49

    CSS MasteryCSS Mastery

    Andy Budd, Simon C...

    New $23.85

    Bulletproof Web DesignBulletproof Web Design

    Dan Cederholm

    New $23.99

    Dreamweaver CS3Dreamweaver CS3

    David McFarland

    New $29.69

    New Perspectives on HTML...New Perspectives on HTML, XHTML, and...

    Patrick Carey

    New $69.26

    HTML, XHTML, and CSS, Si...HTML, XHTML, and CSS, Sixth Edition ...

    Elizabeth Castro

    New $23.09

    jQuery in ActionjQuery in Action

    Bear Bibeault, Yeh...

    New $26.39


    Of Interest:
  • Add to the Beginning of an Array with Unshift in Perl
  • Get the Length of a Word or String in Perl
  • Getting Server Time in Perl
  • Create an Array of Specific Numbers in Perl
  • Use OnMouseOver in Javascript
  • Javascript Lowercase Text
  • Javascript Shift - Use Shift to Remove Items from the Beginning of an Array in Javascript
  • Javascript Bold Text
  • HTML Status
  • HTML Body Tag
  • HTML Font Code
  • HTML Color Codes

  • More Articles:
  • Create an Array of Specific Numbers in Perl
  • Add to the Beginning of an Array with Unshift in Perl
  • Write a FOR LOOP in Perl
  • Exit a Loop with Last
  • onClick Javascript
  • Passing an Argument or Variable to a Javascript
  • Use innerHTML to Change Text on a Page
  • Setting a Cookie using Javascript
  • HTML Codes
  • Make a Line Return Using HTML
  • HTML Font Code
  • Make Text Bold Using HTML

  • Search iLTC w/ Google





    Recent Articles

  • Javascript Focus Form Field
  • Javascript Lowercase Text
  • Javascript Uppercase Text
  • Javascript Superscript Text
  • Javascript Subscript Text
  • Make Text Blink Using Javascript
  • Make a String into a Link in Javascript
  • Javascript Italics Text
  • Javascript Bold Text
  • Javascript Redirect
  • HTML Frames
  • HTML Color Chart
  • HTML Link
  • Javascript Status Bar








  • Perl Programming - Javascript Programming - Maybe Some PHP - Free Scripts - ILoveTheCode
    All Content © 2006 iLoveTheCode.com


    Contact Chris | About this Site


    439,806
    Counter by iLoveTheCode.com


    Last Modified: Friday, 23-Jun-2006 07:23:17 EDT