Click to See Complete Forum and Search --> : quick javascript button and var question.


gs_pistola
08-08-2003, 05:46 AM
i want to be able to have a button on my page that links to a site, and everytime i click it, i increase a var (already declared) by one number in my javascript code. When i tried coding it, everytime i clicked the button it would reset the number to the original "0" i had it initally be. This is my first time attempting anything in javascript. All help appreciated. Thx in advance.

gs_pistola
08-08-2003, 06:13 AM
would i have to use cookies? if so post an example of how to do it. thx

SlankenOgen
08-08-2003, 06:33 AM
If the page with the link relaods the var will reset to 0. Otherwise what you are trying to do is quite feasable.

gs_pistola
08-08-2003, 06:37 AM
how would i prevent the page from reloading after i click the button then?

SlankenOgen
08-08-2003, 06:44 AM
If you put target = "_blank" on the link it will open the new page in a seperate browser window.

gs_pistola
08-08-2003, 06:47 AM
how could i go about this with a cookie

all i need is to store 1 number into a cookie (this is only for local use meaning only gonna be on my computer). And be able to add 1 number to it everyime i call a function to do so. Not to complicated i hope. all help appreciated. thx in advance =)

SlankenOgen
08-08-2003, 06:54 AM
You will have to test it with a server or if you want to do it locally you will have to install a local server on your computer (Sambar or Xitami)

gs_pistola
08-08-2003, 06:58 AM
maybe im not being clear.
what i need is a button in html and basically oncilick it will go to a function in the javascript code and save the number lets say 1 in a cookie. The next time i press the button i want the cookie to have the value 2 in it. What i meant by this being local is its for my local use and not for the web.

SlankenOgen
08-08-2003, 07:01 AM
Yes, but for cookies you need a server, be it a local server on your computer or a remote one.

gs_pistola
08-08-2003, 07:05 AM
ok lets say i create a page. it has the button and the code for creating and reading the cookie number. I save it and place it on my desktop. I run the html file i just created. Now, the code will create a cookie. Lets say i run IE with this html document, wont it create a cookie where it is supposed to be for the default IE cookie directory? I dont need to be running a server do i? Its a simple cookie created by the html document that is run through a browser telling it what info should be in it.

pyro
08-08-2003, 07:39 AM
Originally posted by SlankenOgen
Yes, but for cookies you need a server, be it a local server on your computer or a remote one. Only if you are using a serverside language to write the cookies. If you are using javascript, they will work fine without a server installed.

gs_pistola
08-08-2003, 07:42 AM
exactly, now can u help me with the coding for the cookie plz. just need to + 1 everytime i click on the button. The inital value of the var is 1. all i need is to increase it save it into a cookie then access it and add 1 when i re click the button. thx pyro

gs_pistola
08-08-2003, 07:50 AM
<HEAD>
<script LANGUAGE="JavaScript">
function setCookie()
{
var number = 0;
var the_cookie = "wm_javascript=" + escape("number:" + number);
document.cookie = the_cookie;
readCookie();
}

function readCookie()
{
var the_cookie = document.cookie;
var the_cookie = unescape(the_cookie);
var broken_cookie = the_cookie.split(":");
var number = broken_cookie[1];
number += 1;
}
</script>
</HEAD>

<body>

<form>
<input type="button" value="Set" onClick="setCookie()">
</form>


this is what i have so far, my problem now is i dont have a cookie being saved in my cookies folder. what am i missing. plz respond with an actuall answer to the code that is missing. kthx

pyro
08-08-2003, 07:58 AM
Try this out:

<script type="text/javascript">
num = 1; //the variable we will be working with

/*cookie reading code*/
allcookies = document.cookie;
if (allcookies) {
pos = allcookies.indexOf("number=");
if (pos != -1) {
start = pos + 7;
end = allcookies.indexOf(";", start);
if (end == -1) {
end = allcookies.length;
}
num = allcookies.substring(start, end);
}
}

/*add to number and write cookie*/
function addNum() {
num++
expdate = new Date()
expdate.setFullYear(expdate.getFullYear() + 1);
document.cookie = "number=" + num + "; expires-" + expdate.toGMTString();
alert (num);
}
</script>
</head>
<body>
<form action="">
<p><input type="button" value="Add" onclick="addNum();"></p>
</form>

gs_pistola
08-08-2003, 08:24 AM
thx pyro. that helped so much. Exactly what i wanted. thx again for ur wonderful help. Keep up the good work =)

pyro
08-08-2003, 08:32 AM
You're welcome. Happy to help. :)