...

View Full Version : passing a variable (a number) from one web page to another



xbuddy
01-06-2004, 01:50 AM
Hi, I would like to pass some numbers (which, of course, change from time to time) from one web page to another. For example, let's say I have some memory variables in PAGE1 called:

var numb1=100
var numb2= 200
var numb3=300

I would like to call those numbers up in a new page called PAGE2. How would I call up these memory variable from PAGE1?

I realize the answer is incredibly simple, but I've been searching for the past 2 days and haven't found the answer.

Thanks for your help

glenngv
01-06-2004, 02:53 AM
function go(){
var numb1=100;
var numb2= 200;
var numb3=300;
location.href = "page2.htm?n1=" + numb1 + "&n2=" + numb2 + "&n3=" + numb3;
}

html:
<input type="button" value="Go to Page 2" onclick="go()">

then use this script (http://www.codingforums.com/showthread.php?s=&threadid=11802) to retrieve the querystrings n1, n2, n3 from page 2.

xbuddy
01-07-2004, 02:07 AM
Thanks for taking the time to respond. I REALLY appreciate it. It was so frustrating trying to find this answer.

xbuddy
01-10-2004, 02:29 AM
I put the code on page1 and page2 as instructed. As you can see, the mem variables were transfered to the second page via the location:

file:///M:/Web%20counter/page2.htm?n1=100&n2=200&n3=300

However, when I get to the second page, I am unable to find the variables after they had been extracted. I had assumed the variable names were going to be:

n1
n2
n3

However, they don't seem to work. Am I looking for the wrong varialbe names? If I wanted to display the 3 variables that were transfered and extracted, how would I do that. Maybe that would give me a clue as to what I'm doing wrong.

Thanks again for your help.

Willy Duitt
01-10-2004, 06:27 AM
You may find this (http://www.webxpertz.net/faqs/jsfaq/passvars.php#byname) helpful.

.....Willy

glenngv
01-10-2004, 12:11 PM
Judging from the term "variables" you used to call the extracted data from the url, I suspect that you expect n1, n2 and n3 to be variables that you can use directly in your new page. No, they are not. They are called querystring parameters and you can extract and put them in variables. For basic functionality, here are the codes you only need:


function GetVars( def )
{
this._def_ = def;
var query, queries = top.location.search.substring(1).split( /\&/ );
for ( var i=0; (query = queries[ i ]); i++ )
{
query = query.split( /\=/ );
this[query[0]] = ( typeof query[1] == 'undefined' ) ? def : unescape(query[1]).replace( /\+/g, " " );
}
}

GetVars.prototype.exists = function( key )
{
return ( typeof this[key] != 'undefined' );
}

GetVars.prototype.assign = function( key )
{
return ( this.exists( key ) ) ? this[key] : this._def_;
}

Then to access the querystring parameters:


var _GET = new GetVars( '' );
var num1 = _GET.assign( 'n1' );
var num2 = _GET.assign( 'n2' );
var num3 = _GET.assign( 'n3' );

Now you can use the variables num1, num2, num3 (not n1, n2 and n3) in your codes.

Or for a simpler code (but same method), you can use the code in the link posted by Willy Duitt. But it should be the first topic (http://www.webxpertz.net/faqs/jsfaq/passvars.php#formurl) which you were exactly asking for (not the window.name trick) since on some circumstances (special characters in window.name value, named window, etc) mentioned by the author, that trick does not work.

xbuddy
01-10-2004, 03:49 PM
Thanks Glenn for your help. Sometimes I think that my other programing experience is more of a hinderence than a help. Maybe the way I'm trying to verify that the data was pass is incorrect. Maybe there is a better way to verify it. Anyhow, following is what I had done:

I inserted the coding for the function GetVar. Also, (right below the GetVar function) I inserted the coding to access the query string. However, I am having problems verifying that it was passed. To test it I wrote the following:

document.write(num1);

Nothing was displayed. Any ideas of what i am doing wrong? Should I try to verify it some other way. Since my knowledge of Javascript is limited, I'm having to learn many of the simple programming tricks all over again.

xbuddy
01-10-2004, 04:07 PM
This is a continuation of the previous post. I thought I would show you how I constructed the coding itself in case that was the cause of the problem. Here it is:



<script type="text/javascript" language="javascript">

function GetVars( def )
{
this._def_ = def;
var query, queries = top.location.search.substring(1).split( /\&/ );
for ( var i=0; (query = queries[ i ]); i++ )
{
query = query.split( /\=/ );
this[query[0]] = ( typeof query[1] == 'undefined' ) ? def : unescape(query[1]).replace( /\+/g, " " );
}
}

GetVars.prototype.exists = function( key )
{
return ( typeof this[key] != 'undefined' );
}

GetVars.prototype.assign = function( key )
{
return ( this.exists( key ) ) ? this[key] : this._def_;
}







var _GET = new GetVars( '' );
var num1 = _GET.assign( 'n1' );
var num2 = _GET.assign( 'n2' );
var num3 = _GET.assign( 'n3' );






document.write(num1);


</script>

Garadon
01-10-2004, 04:17 PM
not sure if this help its something I made sort of like glen's

http://www.the-hive.dk/~donp/Library/QueryTest.html

Note if you use please don't direct link to the js copy it to your own page

xbuddy
01-10-2004, 11:11 PM
Sorry for the problems guys, but I did finally get it to work. I'm not sure what happened because I repeated everything I had tried before, but it is working this time.

Thanks

Jeff Mott
01-11-2004, 04:06 AM
then use this script to retrieve the querystrings n1, n2, n3 from page 2There are problems with that script that cause it to incorrectly parse certain query strings. An alternative (http://www.codingforums.com/showthread.php?s=&postid=131863#post131863) is availble. The following discussion to which also details several of the flaws present in other such scripts.

Code Wizard
01-11-2004, 08:47 AM
It's funny u mention only this method,that's because there are other methods too.
i.e. U can use:
var mywin=window.open() //To open the new window
Then u can:
opener.myvar
//...To access a variable
opener.myMethod()
//...To access a method



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum