www.webdeveloper.com
Results 1 to 3 of 3

Thread: Storing Data with Cookies

  1. #1
    Join Date
    Jun 2010
    Posts
    2

    Storing Data with Cookies

    am trying to generate a page-counting cookie that is updated every time someone returns to the site. My code is off I need help. Here are my htm and js files?
    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <!-- 
    New Perspectives on Javascript, 2nd Edition
    Tutorial 9
    Case Problem 1
    
    CycloCrossings Rider Profile
    Author: 
    Date: May 30, 2010
    
    Filename: hangers.htm
    Supporting files: chphoto.jpg, chstyles.css, counter.js, 
    wall.jpg, wall2.jpg
    
    -->
    <title>Cliff Hangers Climbing School</title>
    <link href="chstyles.css" rel="stylesheet" type="text/css" />
    <script src="counter.js" type="text/javascript"></script>
    
    </head>
    
    <body>
    <div id="head"><img src="chlogo.jpg" alt="Cliff Hangers" /></div>
    
    <div id="pageContent">
    <div id="leftCol">
    <ul>
    <li><a href="#">Home Page</a></li>
    <li><a href="#">Our Philosophy</a></li>
    <li><a href="#">Climbing Lessons</a></li>
    <li><a href="#">Tours</a></li>
    <ul>
    <li><a href="#">Petit Grepon</a></li>
    <li><a href="#">Lumpy Ridge</a></li>
    <li><a href="#">North Face</a></li>
    <li><a href="#">Kiener's</a></li>
    <li><a href="#">The Diamond</a></li>
    <li><a href="#">Eldorado</a></li>
    </ul>
    <li><a href="#">Staff</a></li>
    </ul> 
    </div>
    
    <div id="rightCol">
    <div style="float:right; margin: 0px 0px 10px 10px">
    <img src="chphoto.jpg" alt="" />
    </div>
    <p id="firstp">
    Welcome to one of America's best climbing schools located in one of 
    America's best climbing areas, the Colorado Rockies. Nowhere else in 
    America is there such a rich variety of technically 
    challenging rock, ice, and high alpine routes. The area presents the 
    climber with a choice of multiple-pitch climbs and big wall routes. Whether 
    you are here to enjoy the spectacular scenery or to train for an expedition, 
    you can choose no better place than the Colorado Front Range.</p>
    <p>And no one else brings the experience of the Rockies to you better than 
    <span style="font-weight: bold; color: brown">Cliff Hangers Climbing 
    School</span>. Whether you are a beginner, itching to learn the skill and art of 
    climbing, or an experienced mountaineer looking for guides to take you on 
    some of Rocky Mountain National Park's more challenging climbs, consider us 
    your gateway to the mountains.</p>
    <p>We provide five levels of instruction, ranging from Beginner to Advanced,
    including lessons in aid and ice climbing. Members of our experienced staff
    have led expeditions in Mexico, Nepal, and Pakistan. All have intimate
    knowledge of the Colorado Rockies and know the best places to go
    to help you experience the wonder and beauty of the area.</p>
    <p>So contact us today, and start climbing tomorrow!</p>
    
    </div>
    
    </div>
    
    </body>
    
    </html>
    ------------------------------------------------------

    /*
    New Perspectives on Javascript, 2nd Edition
    Tutorial 9
    Case Problem 1

    Author:
    Date: May 30, 2010

    Filename: counter.js



    Functions List:

    addEvent(object, evName, fnName, cap)
    Adds an event handler to object where object is the object
    reference, evName is the name of the event, fnName is the
    reference to the function, and cap specifies the
    capture phase.

    writeDateString(dateObj)
    Returns the date contained in the dateObj date object as
    a text string in the format mmm. dd, yyyy

    storeCookie(cName, cValue, expDate, cPath, cDomain, cSecure)
    Stores a cookie named cName with value cValue. Optional parameters
    expDate, cPath, cDomain, and cSecure set the expiry date,
    path, doman, and secure flag

    getCookie(cName)
    Returns the value of cookie, cName.

    setCounter()
    Retrieves and updates the date last visited and page counter
    cookies and displays them on the Web page along with the
    date last modified.

    */
    Code:
    setCounter()
    function addEvent(object, evName, fnName, cap) {
    if (object.attachEvent)
    object.attachEvent("on" + evName, fnName) ;
    else if (object.addEventListener)
    object.addEventListener(evName, fnName, cap) ;
    }
    
    
    function writeDateString(dateObj) {
    
    var monthName = new Array("Jan", "Feb", "Mar",
    "Apr", "May", "Jun", "Jul", "Aug", "Sep",
    "Oct", "Nov", "Dec");
    
    var thisMonth = dateObj.getMonth() ;
    var thisYear = dateObj.getFullYear() ;
    
    return monthName[thisMonth] + " " + dateObj.getDate() + ", " + thisYear;
    }
    
    function setCookie(cName, cValue, expDate, cPath, cDomain, cSecure) {
    if (cName && cValue != "") {
    var cString = cName + "=" + escape(cValue) ;
    cString += (expDate ? ";expires=" + expDate.toGMTString(): "") ;
    cString += (cPath ? ";path=" + cPath : "");
    cString += (cDomain ? ";domain=" + cDomain : "") ;
    cString += (cSecure ? ";secure" : "");
    document.cookie = cString;
    }
    }
    
    
    function getCookie(cName) {
    if (document.cookie) {
    var cookies = document.cookie.split("; ") ;
    for (var i = 0; i < cookies.length; i++) {
    if (cookies[i].split("=")[0] == cName) {
    return unescape(cookies[i].split("=")[1]) ;
    }
    }
    
    function DeleteCookie (name) { 
    var exp = new Date(); 
    exp.setTime (exp.getTime() - 1) ; 
    // This cookie is history 
    var cval = GetCookie (name); 
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
    <div id="pageFooter">
    <span>Visit Number: pageCount</span>
    <span>lastVisit</span>
    <span>Last Update: pageUpdate</span>
    </div>
    
    }
    
    /* Add new code below */
    
    function writeCookie(cName, cValue, expDate) {
    if (cName && cValue != "") {
    var cString = cName + "=" + escape(cValue);
    if (expDate) cString += ";expires=" + expDate.toGMTString() ;
    if (cPath) cString += ";path=" + cPath;
    if (cDomain) cString += ";domain=" + cDomain ;
    if (cSecure) cString += ";secure";
    document.cookie = cString;
    }
    }
    
    function retrieveCookie(cName) {
    if (document.cookie) {
    var cookiesArray = document.cookie.split ("; ") ;
    for (var i = 0; i < cookiesArray.length; i++) {
    if (cookiesArray[i].split("=")[0] == cName) {
    return unescape(cookiesArray[i].split("=")[1] ) ;
    }
    } 
    
    function deleteCookie(cName) {
    if (document.cookie} {
    var cookiesArray = document.cookie.split ("; ") ;
    for (var i = 0; i < cookiesArray.length; i++) {
    if (cookiesArray[i].split("=")[0] = = cName) {
    document.cookie = cName + "=;expires=Thu, 01-Jan-1970 00:00:01 GMT" ;
    }
    }
    
    function writeCookie(cName, fName, fValue, expDate, cPath, cDomain, cSecure) {
    if (cName && Name && fValue != "") {
    // Create the subKey
    var subKey = fName + "=" + escape (fvalue) ;
    }
    }
    
    function retrieveMCookie (cName, fName) {
    if (document.cookie) {
    // Retrieve the cookie value
    // Retrieve the field value within the cookie
    }
    }
    
    if (retrieveCookie ("lastVisit") = = null) lastVisit = "First time"
    else lastVisit = retrieveCookie ("lastVisit") ;
    
    var today = new Date () ;
    var currentVisit = writeDateString (today) ;
    
    expire = new Date () ;
    expire.setMonth (expire.getMonth () + 6) ;
    writeCookie ("lastVisit", currentVisit, expire) ;
    }
    Last edited by Fang; 06-01-2010 at 01:01 PM.

  2. #2
    Join Date
    Jun 2007
    Posts
    667
    Quote Originally Posted by dmccollins View Post
    am trying to generate a page-counting cookie that is updated every time someone returns to the site.
    It would be easier if you'd host a live test page. You seem to have two sets of cookie functions. This is a basic session counter:
    Code:
    <script type='text/javascript'>
    
    (function( cName, days, prefText, showFirst )
    {
     var cv, dt = new Date(), count = 1, inSession;
       
     if( typeof document.cookie == 'string' )
     {
      inSession = !!document.cookie.match( "\\b"+cName+"SESSION=ok(;|$)" );  
      
      if( (cv = document.cookie.match( "\\b"+cName+"=(\\d+)(;|$)" ) ) )
       count = Number( cv[1] ) + ( inSession ? 0 : 1 );  
       
      if( showFirst || count > 1 ) 
       document.write( prefText + count );  
       
      if( !inSession /*28632953637269707465726C61746976652E636F6D*/ ) 
      {
       document.cookie = cName+"SESSION=ok";
       dt.setDate( dt.getDate() + days );
       document.cookie = cName + '=' + ( cv ? count : 1 ) +';expires=' + dt.toGMTString();
      }      
     }   
    })( "visits", 90, 'Visit count: ', true );   /* <- Parameters  */
    
    </script>
    
    <!-- User Visit Counter ***
    
    Displays optional text followed by the number of visits recorded on the current browser.
    Reloading the document during the same session does not increase the count.
    Requires cookie support.
    
    Installation.
    Either
     Insert this script block at the exact location that the count should appear.
    or
     Save this script as a .js file (without the <script></script> tags), then include it at the required location thus:
     <script type='text/javascript' src='myfile.js'></script>
    
    Parameters.
    
    The four parameters supplied where indicated at the end of the script, have the following meanings:
    Cookie name in quotes, no spaces.
    Days duration. 
    Text appearing before count (in quotes). To display nothing, use "".
    Show count on first visit -  true or false (not in quotes).
    
    -->
    Y_U U_G_A_E_U_ B_S_A_D_

  3. #3
    Join Date
    Jun 2010
    Posts
    2

    Javascript Test Page

    Sterling~

    I am brand new to Javascript and I am not sure what a test page is? I am doing this exercise according to the book so I need to know how to do it per the directions~

    Thanks you so much for any help that you can provide~

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles