Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    10
    Thanked 0 Times in 0 Posts

    How to call a cookie?

    Currently when I hit get cookie it tells me that my cookie is undefined. Any suggestions?

    Html
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    
    
    
    
    <head>
    <script type="text/javascript" src="fadeslideshow.js">
    </script>
    
    	  <link href="stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css" />
      <link href="stylesheets/print.css" media="print" rel="stylesheet" type="text/css" />
      <!--[if IE]>
          <link href="stylesheets/ie.css" media="screen, projection" rel="stylesheet" type="text/css" />
      <![endif]-->
    <link href='http://fonts.googleapis.com/css?family=Italianno' rel='stylesheet' type='text/css'>	
    
    
    </head>
    
    <script type="text/javascript">
    window.onload= text;
    var IdAry=['status1'];
    
    
    </script>
    
    
    
    
    
    <body >
    	<div id="header"> <h1> Rose Photography</h1> </div>
    	<div class="nav">
    <ul>
    <li><a href="index.htm" >Home</a></li>
    <li><a href="gallery.htm">Gallery</a></li>
    <li><a href="404.htm" >Contact</a></li>
    <li><a href="404.htm">XML</a></li>
    </ul>
    </div>
    <div id="login"> 
    <form  name="name" id="vln" action="" method="post" onblur="toMem(this)">
    	Login: <input type="text|hidden" id="txt" name="txt" >
      <input type="button" value="Get Cookie" onclick="ReadCookie()"/>
    </form>
    </div>
    
    	<div class='border-radius-example' id='border-radius'>
      <p id="status1">
    <span id="span1">Find Out How I Got Into Photography!</span>
    <span class="hide">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras eu sapien eget justo porta posuere sollicitudin sed orci. Nunc dictum gravida elit, et sodales velit euismod sed. Nulla facilisi. Cras venenatis, libero ut varius adipiscing, mauris diam tristique eros, quis scelerisque justo enim pulvinar enim. Nulla ante mi, dictum et aliquam ut, interdum at leo. Ut imperdiet, mauris vel tempus euismod, purus nibh gravida urna, eget mattis ante mauris a libero. Maecenas malesuada sagittis justo et gravida. Pellentesque porttitor sollicitudin eleifend. Nulla facilisi. Maecenas feugiat laoreet orci, mattis tempus sem fermentum id. Aenean pretium blandit magna, ac ultrices turpis gravida eget. Proin at bibendum nisl. Phasellus aliquam, enim eu semper adipiscing, turpis lacus tristique nulla, tempor auctor magna odio in justo. Vestibulum vitae purus mauris. Morbi ornare enim sit amet ipsum volutpat a luctus metus condimentum. Quisque aliquet nunc et massa aliquam ut pulvinar justo gravida.
    </span>
    </p>
    
    
    </div>
    
    
    
    
    </body>
    
    </html>


    JS


    Code:
    /// Validate Field
    
    function catchEvent(eventObj, event, eventHandler) {
      if (eventObj.addEventListener) {
        eventObj.addEventListener(event, eventHandler, false);
      } else if (eventObj.attachEvent){
        event = "on" + event;
        eventObj.attachEvent(event, eventHandler);
      }
    }
    catchEvent(window,"load", setupEvents);
    
    function setupEvents(evnt)
    {
    catchEvent(document.getElementById("txt"), "blur", validateField)
    }
     function validateField ()
    {
        var val = this.value.replace(/\D/g, "" ); // zap all NON-digit characters
        if ( val.length != 7 )  /* then there must be 7 digits exactly */
        {
            this.value = '';
      alert("not a vln");
            return false; // ?? may not be needed, can't hurt
        } 
        this.value = val.substr(0,2) + "-" + val.substr(2,2) + "-" + val.substr(4);
        return true;
    }
    
    
    /// Text Functions
    function text() {
     for (var zxc0=0;zxc0<IdAry.length;zxc0++){
      var el=document.getElementById(IdAry[zxc0]);
      if (el){
       el.onmouseover=function() {
         changeText(this,'hide','show')
        }
       el.onmouseout=function() {
         changeText(this,'show','hide');
        }
      }
     }
    }
    function changeText(obj,cl1,cl2) {
       obj.getElementsByTagName('SPAN')[0].className=cl1;
       obj.getElementsByTagName('SPAN')[1].className=cl2;
    }
    
    
    /*
    Cookies
    */
    
    function ReadCookie()
    {
       var allcookies = document.cookie;
       alert("All Cookies : " + allcookies );
    
       // Get all the cookies pairs in an array
       cookiearray  = allcookies.split(';');
    
       // Now take key value pair out of this array
       for(var i=0; i<cookiearray.length; i++){
          name = cookiearray[i].split('=')[0];
          value = cookiearray[i].split('=')[1];
          alert("Key is : " + name + " and Value is : " + value);
       }
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,300
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    I don't see any code to set the cookie. You have only a function ReadCookie().

    It is your responsibility to die() if necessary….. - PHP Manual

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    I don't see any code to set the cookie. You have only a function ReadCookie().

    It is your responsibility to die() if necessary….. - PHP Manual
    I assumed that the value was set when I validated the field and converted the string. :x

  • #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by hcrosex3 View Post
    Currently when I hit get cookie it tells me that my cookie is undefined. Any suggestions?
    It suggests that there are no cookies applicable to the current domain and folder level.

    What do you see if you type into the address bar: javascript:alert(document.cookie) and press Enter?

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by hcrosex3 View Post
    I assumed that the value was set when I validated the field and converted the string. :x
    There is nothing in your code that sets a cookie. Cookies are only set when you include the command to set one.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New Coder
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    10
    Thanked 0 Times in 0 Posts
    I added in this to make sure the cookie was saved and it did work. but for some reason its not saving in the txt box.


    Code:
    this.value = val.substr(0,2) + "-" + val.substr(2,2) + "-" + val.substr(4);
          document.cookie="name=" + this.value;

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,300
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    To set a cookie:-

    Code:
    <script type = "text/javascript">
    
    var expDate = new Date();
    expDate.setTime(expDate.getTime()+365*24*60*60*1000); // one year ahead
    
    function setCookie(isName,isValue,dExpires) {
    document.cookie = isName + "=" + isValue + ";expires=" + dExpires.toGMTString() + "path=/";
    alert (document.cookie);  // for testing
    }
    </script>
    
    <input type = "button" value = 'Set Cookie' onclick = "setCookie('anyName','Hello',expDate)">

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    "Here, cookie, cookie, cookie! Here, cookie!"

    That's how I call them. It usually doesn't work, but that doesn't stop me from trying.

    [Sorry, just couldn't resist, given the title of the thread.]
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New Coder
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    I don't see any code to set the cookie. You have only a function ReadCookie().

    It is your responsibility to die() if necessary….. - PHP Manual
    So I have cookie set but it does not show up in the text box when the page is returned too. any suggestions?


    Code:
    /// Validate Field
    
    function catchEvent(eventObj, event, eventHandler) {
      if (eventObj.addEventListener) {
        eventObj.addEventListener(event, eventHandler, false);
      } else if (eventObj.attachEvent){
        event = "on" + event;
        eventObj.attachEvent(event, eventHandler);
      }
    }
    catchEvent(window,"load", setupEvents);
    
    function setupEvents(evnt)
    {
    catchEvent(document.getElementById("txt"), "blur", validateField)
    }
     function validateField ()
    {
        var val = this.value.replace(/\D/g, "" ); // zap all NON-digit characters
    
    
        if ( val.length != 7 )  /* then there must be 7 digits exactly */
        {
            this.value = '';
      alert("not a vln");
            return false; // ?? may not be needed, can't hurt
        } 
        this.value = val.substr(0,2) + "-" + val.substr(2,2) + "-" + val.substr(4);
          document.cookie='"name=" + this.value;expires=Thu, 2 Aug 2013 20:47:11 UTC; path=/'
    
        return true;
    }
    
    
    /// Text Functions
    function text() {
     for (var zxc0=0;zxc0<IdAry.length;zxc0++){
      var el=document.getElementById(IdAry[zxc0]);
      if (el){
       el.onmouseover=function() {
         changeText(this,'hide','show')
        }
       el.onmouseout=function() {
         changeText(this,'show','hide');
        }
      }
     }
    }
    function changeText(obj,cl1,cl2) {
       obj.getElementsByTagName('SPAN')[0].className=cl1;
       obj.getElementsByTagName('SPAN')[1].className=cl2;
    }
    
    
    /*
    Cookies
    */
    
    function ReadCookie()
    {
       var allcookies = document.cookie;
       alert("All Cookies : " + allcookies );
    
       // Get all the cookies pairs in an array
       cookiearray  = allcookies.split(';');
    
       // Now take key value pair out of this array
       for(var i=0; i<cookiearray.length; i++){
          name = cookiearray[i].split('=')[0];
          value = cookiearray[i].split('=')[1];
          document.getElementById("txt").innerHTML = document.cookie;
    
       }
    }

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,300
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Quote Originally Posted by Old Pedant View Post
    "Here, cookie, cookie, cookie! Here, cookie!"

    That's how I call them. It usually doesn't work, but that doesn't stop me from trying.

    [Sorry, just couldn't resist, given the title of the thread.]
    Aaaarrrgggghhh!

    What do you call a dog with no legs?
    It doesn't matter, he won't come anyway.


    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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