Hi there, I'm trying to use cookies to remember different things on my site. Are there any experts here I ask?
I'm hoping to make images change when clicked and stay that way next time. As well, I would like to be able to add and subtract numbers and save that information as well.
I know ways using javascript to do most of this, yet cookies still have me baffled. Are there simple ways to use cookies? Like a wizard maybe? Something like:
set cookieName to [enter name]
set expiry date to [enter date]
run function called [function name]
because that's basically all it has to do. I've seen it done with changing the background color of the page, but that isn't much help. Has anyone ever seen any useful examples of using cookies?
Any ideas would be greatly appreciated
Comments
: I'm hoping to make images change when clicked and stay that way next time. As well, I would like to be able to add and subtract numbers and save that information as well.
:
: I know ways using javascript to do most of this, yet cookies still have me baffled. Are there simple ways to use cookies? Like a wizard maybe? Something like:
:
: set cookieName to [enter name]
: set expiry date to [enter date]
: run function called [function name]
:
: because that's basically all it has to do. I've seen it done with changing the background color of the page, but that isn't much help. Has anyone ever seen any useful examples of using cookies?
:
: Any ideas would be greatly appreciated
:
Hi there,
Here is my script that I use to handle Cookies. Feel free to use and modify it as you will!
[code]
// cookie.js - Created by Bradley J. Gibby
// Version 2.0 - 06-05-2002
// Changes:
// - A complete rewrite of all the crap that was used from other sites! (ie. EVERYTHING!)
// I can't believe that these people go to SOOOOOOOOO much trouble when it's so simple!
//
function setCookie(name, value, expiry) {
if (!name || !value) { return 0; }
document.cookie = name + "=" + value + ";path=/;expires=" + expiry;
}
function getCookie(name) {
var dc = document.cookie;
var begin = dc.indexOf(name);
var end = (dc.indexOf(";", begin) == -1) ? dc.length : dc.indexOf(";", begin);
return unescape(dc.substring((begin + (name.length + 1)), end));
}
[/code]
I rarely use an Expiration date, but I've just added it quickly for you. It takes a UTC date (check JS Documentation for certain).
Other than that, cookies should be treated as variables inside an object.
[code]
var brad = "Gibby";
setCookie("brad", brad); // Sets a cookie called brad to Gibby
var someguy = getCookie("brad");
alert(someguy); // Display's Gibby.
[/code]
Cookies have been invaluable over the years for me, hope they are as useful to you as they have been to me!
HTH
Bradley q:)
Here's what I did, with your cookie script in there as well.
[code]
function sayGibby ()
{
var brad = "Gibby";
setCookie("brad", brad); // Sets a cookie called brad to Gibby
var someguy = getCookie("brad");
alert(someguy); // Display's Gibby.
}
[/code]
And then in the body
[code]
sayGibby Cookie
[/code]
Yet only showd up as Gibby sometimes. Most of the time the alert was just blank. Any ideas why? I'm sure it's easy for you
I was going to go from there if it worked out, but I don't get it, sorry. If you have some time, could I see another example of how it works? And do you have a site where I can see how you use your cookie scripts?
Thanks for your help though
: Here's what I did, with your cookie script in there as well.
:
: [code]
: function sayGibby ()
: {
: var brad = "Gibby";
: setCookie("brad", brad); // Sets a cookie called brad to Gibby
:
: var someguy = getCookie("brad");
:
: alert(someguy); // Display's Gibby.
:
: }
: [/code]
:
: And then in the body
:
: [code]
: sayGibby Cookie
: [/code]
:
: Yet only showd up as Gibby sometimes. Most of the time the alert was just blank. Any ideas why? I'm sure it's easy for you
: I was going to go from there if it worked out, but I don't get it, sorry. If you have some time, could I see another example of how it works? And do you have a site where I can see how you use your cookie scripts?
: Thanks for your help though
:
Hi there,
Yep, it's just something silly that I shouldn't have done, but hey I'll explain it.
Try this code...
[code]
// cookie.js - Created by Bradley J. Gibby
// Version 2.0 - 06-05-2002
// Changes:
// - A complete rewrite of all the crap that was used from other sites! (ie. EVERYTHING!)
// I can't believe that these people go to SOOOOOOOOO much trouble when it's so simple!
//
function setCookie(name, value) {
if (!name || !value) { return 0; }
document.cookie = name + "=" + value + ";path=/;";
}
function getCookie(name) {
var dc = document.cookie;
var begin = dc.indexOf(name);
var end = (dc.indexOf(";", begin) == -1) ? dc.length : dc.indexOf(";", begin);
return unescape(dc.substring((begin + (name.length + 1)), end));
}
function sayGibby ()
{
var brad = "Gibby";
setCookie("brad", brad); // Sets a cookie called brad to Gibby
var someguy = getCookie("brad");
alert(someguy); // Display's Gibby.
}
sayGibby Cookie
[/code]
If you have a look at the setCookie() function, I've taken out the Expiry feature.
[code]
function setCookie(name, value) {
if (!name || !value) { return 0; }
document.cookie = name + "=" + value + ";path=/;";
}
[/code]
What was happening was that the Expiry was not being set when you called setCookie(), and as you saw, it produced erratic responses. I'm actually baffled as to why it worked at all! I'm going to check into this just for curiousity sake.
See what you think and get back to me!
Cya
Bradley q:)
[code]
Additon of numbers with cookies
// cookie.js - Created by Bradley J. checkHP
// Version 2.0 - 06-05-2002
// Changes:
// - A complete rewrite of all the crap that was used from other sites! (ie. EVERYTHING!)
// I can't believe that these people go to SOOOOOOOOO much trouble when it's so simple!
//
function setCookie(name, value) {
if (!name || !value) { return 0; }
document.cookie = name + "=" + value + ";path=/;";
}
function getCookie(name) {
var dc = document.cookie;
var begin = dc.indexOf(name);
var end = (dc.indexOf(";", begin) == -1) ? dc.length : dc.indexOf(";", begin);
return unescape(dc.substring((begin + (name.length + 1)), end));
}
// Declares basic stats for one type of statistic
var basicStats = 10;
var plus20 = 20; // this is to add 20 to the base stat
var plus40 = 40; // as another test, this will add 40
setCookie("addHP", plus20); // Sets a cookie called addHP to 20
var addedHP = getCookie("addHP");
var currentHP = basicStats + addedHP;
var mathHP;
mathHP = eval(currentHP);
document.write("You're current HP: ");
document.write(mathHP);
document.write("");
function checkHP ()
{
alert(currentHP); // Display's current HP with cookie additon.
}
checkHP with Cookie
[/code]
Either way of getting the result only shows the answer as 1020. Not 30 like it's supposed to be. Do you know how to change a cookie that is set to 20, to actually be the number 20? And then add them together?
I found that I could multiply them, and it works out as 200. Or divide them too. That all works, so why isn't a simple plus sign adding the numbers together? It only prints both values out separately. I assume there is a way to change the cookie, or a different way to add numbers than what I'm doing. I hope it's simple ^_^ But I can't find out how to do that anywhere.
Anyways, thanks for your help, it's been very useful so far. I just keep hitting snags like this.
A lot of problems occur when JavaScript treats you numeric values as strings. What's happening here is the result of your addition is actually a concatenation!
You need to tell JS that you want to treat the values as numbers.
Try this...
[code]
var currentHP = Math.abs(basicStats) + Math.abs(addedHP);
[/code]
instead of your current line.
HTH
Bradley q:)
:
Yeah I kinda remembered from the last time ya told me q:)
*grin*