By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,481 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,481 IT Pros & Developers. It's quick & easy.

Splitting cookie

P: n/a
Can anyone help with this problem. I am attempting to dynamically draw a
graph using data from a cookie. I have written the script to actually draw
the graph, for which I hav created two arrays with the data I required
preset into them. What I wish to do is split the cookie in which the data
is stored and create two array from it.

The function used to create the cookie is as follows

:function SetCookie(name, value) {
document.cookie = name + "=" + escape(value);
}

which is called with the following script:

<body onload="SetCookie('sales','pan1=23,pan3=153,pan4=2 1,pan9=5')">

I wish to create the following arrays by splitting the cookie:

array1=("sales","pan1","pan3","pan4","pan9")
array2=(23,153,21,5)

Can anyone help? Many thanks in advance.

Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
"Steve Darby" <ra****************@ntlworld.com> wrote in message
news:cY**************@newsfe6-win.ntli.net...
Can anyone help with this problem. I am attempting to dynamically draw a
graph using data from a cookie. I have written the script to actually draw the graph, for which I hav created two arrays with the data I required
preset into them. What I wish to do is split the cookie in which the data
is stored and create two array from it.

The function used to create the cookie is as follows

:function SetCookie(name, value) {
document.cookie = name + "=" + escape(value);
}

which is called with the following script:

<body onload="SetCookie('sales','pan1=23,pan3=153,pan4=2 1,pan9=5')">

I wish to create the following arrays by splitting the cookie:

array1=("sales","pan1","pan3","pan4","pan9")
array2=(23,153,21,5)

Can anyone help? Many thanks in advance.


What does your function "SetCookie()" look like?
Jul 23 '05 #2

P: n/a
Like this:

function SetCookie(name, value) {
document.cookie = name + "=" + escape(value);
}

Jul 23 '05 #3

P: n/a
"Steve Darby" <ra****************@ntlworld.com> wrote in message
news:In***************@newsfe6-win.ntli.net...
Like this:

function SetCookie(name, value) {
document.cookie = name + "=" + escape(value);
}


If you do:
alert(unescape(document.cookie));

do you get this?
sales=pan1=23,pan3=153,pan4=21,pan9=5

Jul 23 '05 #4

P: n/a
"Steve Darby" <ra****************@ntlworld.com> wrote in message
news:In***************@newsfe6-win.ntli.net...
Like this:

function SetCookie(name, value) {
document.cookie = name + "=" + escape(value);
}

Are you sure that you don't want:

array1=("pan1","pan3","pan4","pan9")
array2=(23,153,21,5)

isntead of:

array1=("sales","pan1","pan3","pan4","pan9")
array2=(23,153,21,5)

thus array1[0] and array2[0] will correspond to "pan1" and "23".
Jul 23 '05 #5

P: n/a
I need 'sales' to be in array1 as well as the rest as it is used to form the
heading of the bar chart
Jul 23 '05 #6

P: n/a
"Steve Darby" <ra****************@ntlworld.com> wrote in message
news:aH**************@newsfe6-win.ntli.net...
I need 'sales' to be in array1 as well as the rest as it is used to form the heading of the bar chart

Will this work for you?

<html>
<head>
<title>namevals.htm</title>
<script type="text/javascript">
var array1 = new Array();
var array2 = new Array();
function GetCookie() {
var what = unescape(document.cookie);
array1[0] = what.substr(0,what.indexOf("="));
what = what.substr(what.indexOf("=")+1);
var pair = what.split(",");
var j = 1;
var k = 0;
for (var i=0; i<pair.length; i++) {
var item = pair[i].split("=");
array1[j++] = '"' + item[0] + '"';
array2[k++] = item[1];
}
// display contents of both arrays:
what = unescape(document.cookie);
what += "\n\n array1 = " + array1.join(",");
what += "\n\n array2 = " + array2.join(",");
alert(what);
}
function SetCookie(name,value) {
document.cookie = name + "=" + escape(value);
}
</script>
</head>
<body onload="SetCookie('sales','pan1=23,pan3=153,pan4=2 1,pan9=5')">
<a href="javascript:GetCookie()">Get Cookie</a>
</body>
</html>
Jul 23 '05 #7

P: n/a
Will try it and let you know

"McKirahan" <Ne**@McKirahan.com> wrote in message
news:IL********************@comcast.com...
"Steve Darby" <ra****************@ntlworld.com> wrote in message
news:aH**************@newsfe6-win.ntli.net...
I need 'sales' to be in array1 as well as the rest as it is used to form

the
heading of the bar chart

Will this work for you?

<html>
<head>
<title>namevals.htm</title>
<script type="text/javascript">
var array1 = new Array();
var array2 = new Array();
function GetCookie() {
var what = unescape(document.cookie);
array1[0] = what.substr(0,what.indexOf("="));
what = what.substr(what.indexOf("=")+1);
var pair = what.split(",");
var j = 1;
var k = 0;
for (var i=0; i<pair.length; i++) {
var item = pair[i].split("=");
array1[j++] = '"' + item[0] + '"';
array2[k++] = item[1];
}
// display contents of both arrays:
what = unescape(document.cookie);
what += "\n\n array1 = " + array1.join(",");
what += "\n\n array2 = " + array2.join(",");
alert(what);
}
function SetCookie(name,value) {
document.cookie = name + "=" + escape(value);
}
</script>
</head>
<body onload="SetCookie('sales','pan1=23,pan3=153,pan4=2 1,pan9=5')">
<a href="javascript:GetCookie()">Get Cookie</a>
</body>
</html>

Jul 23 '05 #8

P: n/a
Have run the script and the alert returns the following:

array1=,"","pan3","pan4","pan9"
array2=,153,21,5;style

And I should have mention that I am using script which allows me to change
text size on the page and saves the prefered values as a cookie, hence the
appearance of style in the second array. I am trying only to the
information listed for sales, if that makes sense
Jul 23 '05 #9

P: n/a
"Steve Darby" <ra****************@ntlworld.com> wrote in message
news:iY***************@newsfe6-win.ntli.net...
Have run the script and the alert returns the following:

array1=,"","pan3","pan4","pan9"
array2=,153,21,5;style

And I should have mention that I am using script which allows me to change
text size on the page and saves the prefered values as a cookie, hence the
appearance of style in the second array. I am trying only to the
information listed for sales, if that makes sense

Would you change
alert(what);
to
document.write(what);
and post the results here.
Jul 23 '05 #10

P: n/a
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:JZ********************@comcast.com...
"Steve Darby" <ra****************@ntlworld.com> wrote in message
news:iY***************@newsfe6-win.ntli.net...
Have run the script and the alert returns the following:

array1=,"","pan3","pan4","pan9"
array2=,153,21,5;style

And I should have mention that I am using script which allows me to change text size on the page and saves the prefered values as a cookie, hence the appearance of style in the second array. I am trying only to the
information listed for sales, if that makes sense

Would you change
alert(what);
to
document.write(what);
and post the results here.

Rather just add the following after the alert():

what = unescape(document.cookie);
what += "<br><br> array1 = " + array1.join(",");
what += "<br><br> array2 = " + array2.join(",");
document.write(what);
Here's what I get:

sales=pan1=23,pan3=153,pan4=21,pan9=5

array1 = sales,"pan1","pan3","pan4","pan9"

array2 = 23,153,21,5
Jul 23 '05 #11

P: n/a
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:JZ********************@comcast.com...
"Steve Darby" <ra****************@ntlworld.com> wrote in message
news:iY***************@newsfe6-win.ntli.net...
Have run the script and the alert returns the following:

array1=,"","pan3","pan4","pan9"
array2=,153,21,5;style

And I should have mention that I am using script which allows me to change text size on the page and saves the prefered values as a cookie, hence the appearance of style in the second array. I am trying only to the
information listed for sales, if that makes sense


I forgot the first element was supposed to quoted; try this:

<html>
<head>
<title>namevals.htm</title>
<script type="text/javascript">
var array1 = new Array();
var array2 = new Array();
function GetCookie() {
var what = unescape(document.cookie);
array1[0] = '"' + what.substr(0,what.indexOf("=")) + '"';
what = what.substr(what.indexOf("=")+1);
var pair = what.split(",");
var j = 1;
var k = 0;
for (var i=0; i<pair.length; i++) {
var item = pair[i].split("=");
array1[j++] = '"' + item[0] + '"';
array2[k++] = item[1];
}
what = unescape(document.cookie);
what += "<br><br> array1 = " + array1.join(",");
what += "<br><br> array2 = " + array2.join(",");
document.write(what);
}
function SetCookie(name,value) {
document.cookie = name + "=" + escape(value);
}
</script>
</head>
<body onload="SetCookie('sales','pan1=23,pan3=153,pan4=2 1,pan9=5')">
<a href="javascript:GetCookie()">Get Cookie</a>
</body>
</html>
You should get this:

sales=pan1=23,pan3=153,pan4=21,pan9=5

array1 = "sales","pan1","pan3","pan4","pan9"

array2 = 23,153,21,5
Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.