...

View Full Version : My global vars cause problems :( Newer than newbie



joshuafalken
07-13-2012, 11:38 AM
I am trying to pass values from one page to another using a cookie. I am writing a trivia game for couples. The female player enters her name and then presses a button to go to the male player screen.

A cookie is written with the female player value and it all seems to work ok. But when the male player reads the cookie values into global vars something seems to go wrong when his cookie is getting written. I've debugged a bit but cannot figure out why the third alert in the writecookie does not show up.

This is the script as it appears in my page html between the head tags. Note that readcookie fires on onload and writecookie works from a button click. Both scripts seem to activate, but the write one does not show the third alert.

I am brand new to this and am really enjoying it. I have a lot of experience in homebrew and self taught Delphi but want to broaden my experience. I am loving adding effects to web pages and images using javascript. Wish I'd done it years ago.


<script type="text/javascript">
function readCookie()
{
if (document.cookie != "") //make sure the cookie exists
{
noName = document.cookie.split("=")[1];
yourName = document.cookie.split("=")[2];
yourName2 = document.cookie.split("=")[3];
document.getElementById('female_player_name').innerHTML="<b>"+yourName2+"'s Personal Settings</b>";

alert("Read Cookies : " + ">" + noName+ yourName+">"+yourName2);

}
else
{
var yourName2 = "Gillian NotFound";
alert("Cookie not found!");

document.getElementById('female_player_name').innerHTML="<b>"+yourName2+"'s Personal Settings</b>";
}
}
function WriteCookie()
{
alert("Hello");
alert("Hello2");
alert(yourName+">"+yourName2);
}
</script>

Philip M
07-13-2012, 12:34 PM
Your function WriteCookie() does nothing except show some alerts.

noName = document.cookie.split("=")[1];
yourName = document.cookie.split("=")[2];
yourName2 = document.cookie.split("=")[3];
is not correct.

var c = document.cookie.split("=");
var noname = c[0]; // array index starts at 0
var yourName = c[1];
var yourName2 = c[2];

But you should not use = as a delimiter. Use ~ or whatever.



Cookies have been covered very many times in this forum - try using the search feature for working examples. That will probably save you a lot of time.

joshuafalken
07-13-2012, 01:55 PM
Thanks so much. I am digesting this now and trying to figure out how to apply it.

R

joshuafalken
07-13-2012, 05:17 PM
Only thing I am having trouble with now is how to make sure I am reading the same cookie I wrote. It seems to be reading an older one with a different (old) name?

How do I specify the cookie to read:


<script type="text/javascript">
function readCookie()
{
if (document.cookie != "") //make sure the cookie exists
{
var c = document.cookie.split("~");
var noname = c[0]; // array index starts at 0
var yourName = c[1];
var yourName2 = c[3];
document.getElementById('female_player_name').innerHTML="<b>"+yourName2+"'s Personal Settings</b>";

alert("Read Cookies : ><"+noname+ "><" + yourName+"><"+yourName2);

}
else
{
var yourName2 = "Gillian NotFound";
alert("Cookie not found!");

document.getElementById('female_player_name').innerHTML="<b>"+yourName1+"'s Personal Settings</b>";
}
}
</script>

Philip M
07-13-2012, 05:19 PM
function getCookie(isName){
cookieStr = document.cookie;
startSlice = cookieStr.indexOf(isName+"=");
if (startSlice == -1) {return false} // the cookie was not found
endSlice = cookieStr.indexOf(";",startSlice+1)
if (endSlice == -1){endSlice = cookieStr.length}
isData = cookieStr.substring(startSlice,endSlice);
isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return isValue;
}

Naturally you must specify the name of the cookie to be retrieved.

joshuafalken
07-13-2012, 06:24 PM
Okay I think I have asked enough dumb questions for now. I really appreciate your patience and thank you for your time.

I'll be working furiously hard over the summer hols to get to grips with all of this.

R

Philip M
07-13-2012, 07:54 PM
Okay I think I have asked enough dumb questions for now. I really appreciate your patience and thank you for your time.

I'll be working furiously hard over the summer hols to get to grips with all of this.

R

That is very gracious of you. :) We are all keen to help, but of course you must (as you recognise) study the topic as well. Good luck!

joshuafalken
07-14-2012, 03:44 PM
I've been playing with the getcookie function and the only query I have so far is this:

I read and then rewrite the cookie on each page (onload = getcookie and exit page = writecookie). Is the cookie overwritten or appended to? I want to create it from scratch each time but it seems to be getting appended.

thanks

R

Philip M
07-14-2012, 04:02 PM
I've been playing with the getcookie function and the only query I have so far is this:

I read and then rewrite the cookie on each page (onload = getcookie and exit page = writecookie). Is the cookie overwritten or appended to? I want to create it from scratch each time but it seems to be getting appended.

thanks

R

Normally setting a cookie of the same name will over-write the previous one. But you do not show your writecookie() code.

joshuafalken
07-14-2012, 04:26 PM
I'm looking into using the Query String solution to pass the data from page to page now, as this seems more logical to me considering that I want to accumulate data that will be passed in by the users in 3 different pages. I was trying to rewrite the cookie after each page, but I found that I was getting old values.

I'm still learning (will be for years now I guess) and realise that a server side script might also be what i need. But I would like to know why my cookie is not being changed when I write it in the exitting page and read it in the new page.

Anyway, here's my write:

<script>
function WriteCookie()
{
cookievalue2= "spsettings="+escape(yourName)+"="+escape(yourName2)+";";

document.cookie=cookievalue2;
alert("Setting Cookies :>" + cookievalue2 );
location="game_setup.html";
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum