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



  • I bookmarked this site...
    20,084

    iLoveTheCode.com
    Javascript > Javascript Tutorials How To Easy > Setting a Cookie using Javascript

    Setting a Cookie using Javascript



    To set cookies in Javascript use document.cookie

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

    -You named your cookie iltccook because you like the way it sounds.
    -You give it a value of got it because you think it's clever.
    -When a user accesses your page that contains this script, they will get a cookie that lasts until 2036.


    NOTES:
    -The domain value must be your domain or subdomain (the domain where the script is).
    -The time must be in this format Mon, 18-Feb-2036 05:43:38 GMT
    -20 cookie per domain limit


    You can give the cookie more than one value like so:

    var huhu="got it,got more,and still more";

    document.cookie="iltccook=" + huhu + ";domain=ilovethecode.com;expires=Mon, 18-Feb-2036 05:43:38 GMT;path=/";


    NOTES:
    -We use plus symbols to insert our variable huhu into the cookie.
    -Cookies with the same name overwrite the previous cookie.


    To print the cookie back to the browser:

    var ko=document.cookie;

    document.write(ko);

    or

    document.write(document.cookie);


    To see your all your cookies:
    Mac Firefox - Preferences > Privacy > View Cookies > Then search for your domain
    Mac Safari - Preferences > Advanced > Show Cookies
    Windows Firefox - Tools > Options... > Privacy > Cookies > View Cookies
    Windows IE - Tools > Internet Options... > General (Tab) > Settings... > View Files...


    Free Copy and Paste Javascript Code (change domain to your domain; change name and value if you like):

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

    //set cookie number one - iltccook

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


    //set cookie number two - iltccook2

    var huhu="got it,got more,and still more";

    document.cookie="iltccook2=" + huhu + ";domain=ilovethecode.com;expires=Mon, 18-Feb-2036 05:43:38 GMT;path=/";


    //print cookies set by this domain

    document.write(document.cookie);

    -->
    </script>



    Resources:
    Formatting Time in Javascript
    Reading Cookies using Javascript
    Search Google for "Javascript Set Cookie"
    Microsoft - Specs for Date and Time Formatting
    Web Monkey Cookie Tutorial
    Spltting an Array in Javascript


    Click HERE to comment or discuss at iLoveTheCode GOOGLE Group
    Previous Article: Splitting an Array using Javascript
    Next Article: Reading Cookies using Javascript





    Pro CSS and HTML Design ...Pro CSS and HTML Design Patterns

    Michael Bowers

    New $29.69

    Head First HTML with CSS...Head First HTML with CSS & XHTML

    Eric Freeman, Elis...

    New $26.39

    Bulletproof Web DesignBulletproof Web Design

    Dan Cederholm

    New $26.39

    CSS MasteryCSS Mastery

    Andy Budd, Simon C...

    New $23.09

    Learning Web DesignLearning Web Design

    Jennifer Niederst ...

    New $29.69

    Struts 2 in ActionStruts 2 in Action

    Don Brown, Chad Da...

    New $29.69

    jQuery in ActionjQuery in Action

    Bear Bibeault, Yeh...

    New $26.39

    DojoDojo

    Matthew A. Russell...

    New $26.39


    Of Interest:
  • What's in an Array, Index Numbers
  • And, Or, Equal, Not Equal, Greater Than, Less Than in Perl
  • Making a Directory Using mkdir in Perl
  • What Environmental Variables are Available
  • Increment a Variable in Javascript
  • Opening an Alert Box in Javascript
  • PRINT to Browser with document.write Command in Javascript
  • Substitute in Javascript Using replace Method
  • HTML Status
  • HTML Space
  • HTML Body Tag
  • Make a Line Return Using HTML

  • More Articles:
  • If, Elsif, and Else in Perl
  • Substitute in Perl
  • Exit a Loop with Last
  • Encryption with the Crypt Function in Perl
  • onClick Javascript
  • Annoy Users with onBlur in Javascript
  • Change a Form Field with Javascript
  • Substitute in Javascript Using replace Method
  • Opening a REMOTE WINDOW in Javascript
  • HTML Subscript Text
  • HTML Font Code
  • Make a New Paragraph Using HTML
  • Make Italic Text 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


    316,824
    Counter by iLoveTheCode.com


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