PDA

View Full Version : Passing forward variables



Hannes
03-29-2005, 05:10 PM
Hi,

I found a nice script by jscheuer1 in the html forum that is an example of passing variables (or or better said their values) from one window to another by javascript.

Now what if I want to use these values and then pass it forward to one more window? I tried it in the following way but it does not work.

What I tried to do was to return the value of the variable with a function (givethevalue) and so using it outside the actual javascript routine. I know that what I need is the proper syntax for the last code line but can not find out. Could you please help?

File 1:
<html>
<head>
<title>javascript variable passing - page 1</title>
</head>
<a href="ss.htm?var1=33">Send Variable</a>
</body>
</html>


File 2, named ss.htm:
<html>
<head>
<title>javascript variable passing - page 2</title>
</head>
<body>
<form name="recieve">
<input type="hidden" name="var1">
</form>
<script language="javascript">
var locate = window.location;
document.recieve.var1.value = locate;
var text = document.recieve.var1.value;
function delineate(str) {
theleft = str.indexOf("=") + 1;
theright = str.indexOf("&");
return(str.substring(theleft, str.length));
}

function givethevalue() {
text2 = "";
text2 = "tt.htm?" + delineate(text);
return(text2);
}
</script>

<p>The passed variable can now be passed to a new file:</p>
<p><a href="javascript:givethevalue()">New window</a></p>

</body>
</html>

Also I tried this. Does not work either:

function givethevalue() {
text2 = "";
text2 = delineate(text);
return(text2);
}
</script>

<p>The passed variable can now be passed to a new file:</p>
<p><a href="tt.htm?+javascript:givethevalue()">New window</a></p>

</body>
</html>

jscheuer1
03-29-2005, 08:03 PM
Instead of going through all that, from my original example for page 2:


<html>
<head>
<title>javascript variable passing - page 2</title>
</head>
<body>
<form name="recieve">
<input type="hidden" name="var1">
</form>
<script language="javascript">
var locate = window.location;
document.recieve.var1.value = locate;
var text = document.recieve.var1.value;
function delineate(str) {
theleft = str.indexOf("=") + 1;
theright = str.indexOf("&");
return(str.substring(theleft, str.length));
}
document.write('<a href="nextpage.htm?var1=' +delineate(text)+ '">New Window</a>');
</script>
</body>
</html>

Hannes
03-29-2005, 09:13 PM
Now I understand:

Use document.write and let this work inside the javascript. I built this into my routine and it works like a charm.

Thank you again!