...

View Full Version : Need help checking for cookies...



disneyfan
03-14-2005, 04:35 PM
Again I'm working through this old javascript book a friend lended me trying to learn some of this on my own...and I've ran across a stumbling block with cookies.

I want to create a function that checks to see if a cookie exists... and if so, split it in individual parts and display then through an alert()

can someone help me out... I'm not really even sure where to begin

vwphillips
03-14-2005, 04:57 PM
you have not replied to the last reply I made to one of your posts

and cookies should be addressed after the basics have been mastered

but I just posted this on another forum




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>
<title></title>

<script language="JavaScript" type="text/javascript">
<!--
var CookieAry=new Array('tom1','bill0','dick','harry','bill','tomee');
var FindAry=new Array();
var v;

function FindCookieValue(){
if (!pv_GetCookie()){ return; }
v=pv_GetCookie();
for (i=0;i<CookieAry.length;i++){
if (CookieAry[i].match(v)){
FindAry[FindAry.length]=CookieAry[i];
}
}
alert('Found '+FindAry);
}

var pv_exp=new Date(new Date().getTime()+.1*86400000);
var pv_Cookie='VicX';

function pv_GetCookie(){
var pv_st=document.cookie.indexOf(pv_Cookie+"=");
var pv_len=pv_st+pv_Cookie.length+1;
if ((!pv_st)&&(pv_Cookie != document.cookie.substring(0,pv_Cookie.length))) return null;
if (pv_st==-1) return null;
var pv_end=document.cookie.indexOf(";",pv_len);
if (pv_end==-1) pv_end=document.cookie.length;
return unescape(document.cookie.substring(pv_len,pv_end));
}


function pv_SetCookie(value,expires) {
document.cookie=pv_Cookie+"="+value+";expires="+expires+";path=/;"
}


//-->
</script>
</head>

<body onload="FindCookieValue();" onunload="pv_SetCookie('tom',pv_exp);" >
<input id=h name="" size="10">
</body>

</html>

disneyfan
03-14-2005, 06:18 PM
what msg did I not reply to...

actually I've been moving along pretty good in the book and I'm not on cookies...

what does the code you posted do exactly?

disneyfan
03-15-2005, 11:46 AM
ok here's what I have so far... but I'm getting an error. What am I missing?

<HTML>
<HEAD>
<TITLE>Cookies</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
function showCookie() {
if(GetCookie("name") == null) {
alert("No cookies found!");
}

// else this is second or greater visit
else {
document.cookie = encodeURI("name=" + document.cookieExample.name.value);
document.cookie = encodeURI("date=" + document.cookieExample.date.value);
var savedData = decodeURI(document.cookie);
var dataArray = savedData.split("; ");
var messageString = "";
for (var i = 0; i < dataArray.length; ++i) {
messageString += dataArray[i] + "\n";
}
alert(messageString);
return false;
}
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>
</HEAD>
<BODY>
<H3>Cookies</H3>
<FORM NAME="cookieExample" onReset="return confirmReset();" onSubmit="return showCookie();">
<P><B>Name</B><BR>
<INPUT TYPE="text" NAME="name" SIZE=50></P>
<P><B>Date</B><BR>
<INPUT TYPE="text" NAME="date" SIZE=50></P>
<P><INPUT TYPE="reset">
<INPUT TYPE="submit"></P>
</FORM>
</BODY>
</HTML>

disneyfan
03-15-2005, 01:08 PM
:confused:

martijntje
03-15-2005, 01:11 PM
Maybe you should post the exact error you are getting?

disneyfan
03-15-2005, 01:38 PM
ok...

object expected on line 7 which is

if(GetCookie("name") == null) {

martijntje
03-15-2005, 01:46 PM
GetCookie is not a javascript function AFAIK.
I believe you can access cookies through document.cookie

disneyfan
03-15-2005, 01:52 PM
you mean like this?

if(document.cookie("name") == null) {

I don't think this is working either... any other ideas?

martijntje
03-15-2005, 02:02 PM
document.cookie isn't an array, so what you're doing is absolutely incorrect, you're correct in that. :p

document.cookie is the raw cookie data. You need to process is into something useful. I suggest you search google or maybe try an "alert(document.cookie);" to see how it's buildup.

I believe you are trying to use the cookie function declare here: http://www.netspade.com/articles/javascript/cookies.xml

The functions look interesting, but of course you shouldn't forget to include those functions in your page if you intent to use them.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum