...

View Full Version : How to call a cookie?



hcrosex3
12-06-2012, 04:15 PM
Currently when I hit get cookie it tells me that my cookie is undefined. Any suggestions?

Html
<!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



/// 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);
}
}

Philip M
12-06-2012, 05:27 PM
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

hcrosex3
12-06-2012, 05:48 PM
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

Logic Ali
12-06-2012, 05:55 PM
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?

felgall
12-06-2012, 06:28 PM
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.

hcrosex3
12-06-2012, 07:54 PM
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.



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

Philip M
12-06-2012, 08:01 PM
To set a cookie:-


<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)">

Old Pedant
12-06-2012, 08:37 PM
"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.]

hcrosex3
12-06-2012, 09:07 PM
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?



/// 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;

}
}

Philip M
12-07-2012, 07:47 AM
"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.

:D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum