Click to See Complete Forum and Search --> : passing on a variable


ahasverus
09-29-2003, 08:41 AM
I know it is possible to pass a variable to another page with javascript using a '?' in the url.

I remember I read about it on the htmlgoodies website but I can't find it anymore.

Does anyone kwon how it works?

Thank you.

Khalid Ali
09-29-2003, 09:14 AM
apend the variable value to the pages url where you wnat to recieve the value,
Suppose the next paes url is
var url = "http://somedomain.com/pages/secondpage.html";

now you have a value in variable

data = "FirstPageIsSeen";

appended url will look like this
url+="?dataFromLastPage="+data;

in the second page use

datafromLastPage = window.location.search;

requestcode
09-29-2003, 09:30 AM
You might also find this helpful:
http://www.javascriptkit.com/javatutors/send1.shtml

ahasverus
09-29-2003, 09:36 AM
Thanks, it works, the variable passes.

But I still got a little problem. I want to pass an integer number and it seems that when I perform an operation on the passed integer it acts as a string. (if I pass 2 and perform 2+1 I get 21)

How can I convert it back to an integer or make sure it gets passed as an integer?

Khalid Ali
09-29-2003, 09:38 AM
parseInt(value)
or Number(value)

ahasverus
09-29-2003, 09:52 AM
Thanks, but I still can't get it to work.

What I want is to show a series of pictures, the javascript on the page determines the number of the first picture and shows 5 pictures. Under the pictures I have a line with links like this:

previous 1 2 3 4 ... next

with these links I pass the number. Clicking on the numbers works because I know wich number to pass in the link. So I reload the page and pass this number to it.

Clicking next I add 1 to the number that was passed to the current page and reload the page with this new number passed. But then it seems to treat the number as a string even if I use parseInt or Number.

Clicking previous always gets the result NaN (so the number is undefined)

Here is the code:


var x=1;
var z=1;
function findstring(){
parameters=document.location.search;
if (parameters){
param = parameters.split("num=");
x = param[1]
Number(x)
}

z = ((x-1)*5)+1;

schrijfcode();
}

aantal = 20

function schrijfcode(){
document.write("<center><table>");
var y;
y=z;

document.write("<th>foto's "+y+" tot "+(y+4)+"</th>")
for (y=z; y<z+5; y++) {
if (y<95){
document.write("<tr><td><a href='"+ y +".JPG'><img src='"+ y +".JPG' border='0' width='409' height='307'></a></td></tr>")
}
}
document.write("</table></center><br><br>");
writenumbers();
}

function writenumbers(){
document.write("<center>")
if (x!=1){
document.write("<a href='javascript:vorige()'>vorige</a>");
}
for (j=1; j<aantal; j++){
document.write("&nbsp;<a href='javascript:ganaar("+j+")'>"+j+"</a>&nbsp;")
}
if (x<19){
document.write("<a href='javascript:volgende()'>volgende</a>");
}
document.write("</center>")
}

function vorige(){
nummer = x-1;
window.location.href = eval("'index.htm?num"+ nummer +"'");
}

function volgende(){
nummer=0;
nummer =x+1;
window.location.href = eval("'index.htm?num="+ nummer+"'");
}

function ganaar(i){
window.location.href = eval("'index.htm?num="+i+"'");
}

findstring();


I can't get it to work I really hope someone can find what's wrong with it.

Khalid Ali
09-29-2003, 10:09 AM
you do not need to use eval wher eyou have used, and your code is too confusing,can you try to reduce the reliance on JavaScript a bit?