Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-06-2012, 04:15 PM   PM User | #1
hcrosex3
New Coder

 
Join Date: Sep 2012
Posts: 32
Thanks: 10
Thanked 0 Times in 0 Posts
hcrosex3 is an unknown quantity at this point
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);
   }
}
hcrosex3 is offline   Reply With Quote
Old 12-06-2012, 05:27 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 12-06-2012, 05:48 PM   PM User | #3
hcrosex3
New Coder

 
Join Date: Sep 2012
Posts: 32
Thanks: 10
Thanked 0 Times in 0 Posts
hcrosex3 is an unknown quantity at this point
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
hcrosex3 is offline   Reply With Quote
Old 12-06-2012, 05:55 PM   PM User | #4
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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?
Logic Ali is offline   Reply With Quote
Old 12-06-2012, 06:28 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 12-06-2012, 07:54 PM   PM User | #6
hcrosex3
New Coder

 
Join Date: Sep 2012
Posts: 32
Thanks: 10
Thanked 0 Times in 0 Posts
hcrosex3 is an unknown quantity at this point
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;
hcrosex3 is offline   Reply With Quote
Old 12-06-2012, 08:01 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 12-06-2012, 08:37 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
"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.
Old Pedant is offline   Reply With Quote
Old 12-06-2012, 09:07 PM   PM User | #9
hcrosex3
New Coder

 
Join Date: Sep 2012
Posts: 32
Thanks: 10
Thanked 0 Times in 0 Posts
hcrosex3 is an unknown quantity at this point
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;

   }
}
hcrosex3 is offline   Reply With Quote
Old 12-07-2012, 07:47 AM   PM User | #10
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:42 AM.


Advertisement
Log in to turn off these ads.