Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variables 3

Status
Not open for further replies.

JimJx

Technical User
Feb 16, 2001
202
US
I have an HTML form that passes a variable to a included .js file and opens another page based on the selections made in the form. The same .js file is being used in the second page, as are many of the same functions, since the second page is another form and I have one more page that I need to access after that.

Is there anyway, short of using a cookie, to get the variables passed? I know that it can be a problem to get variables between pages, but I thought the included .js file may be a way around it.

Any other ideas are greatly appreciated.
Jim



 
there is a qay to do it. You can place the variable, if its a string or a number, in the querystring of the url, like so:

mypage.htm?variabletopass=somevalue

and then mypage.htm can access it. You can manually access it using the window.location.search property, or use this object I created to handle it:

function QSHandler()
{
var i,j,tmparray,strata,prlen
strata = window.location.search
strata = strata.substring(1,strata.length)
if(strata.indexOf('&')>=0)
{
prsarray = strata.split('&')
prlen = prsarray.length
tmparray = new Array()
for(t=j=0;j<prlen;j++)
{
tmparray[t++] = prsarray[j].split('=')[0]
tmparray[t++] = prsarray[j].split('=')[1]
}
}
else
{
tmparray = new Array(strata.split('=')[0],strata.split('=')[1])
}
tlen = tmparray.length
this.data = new Array()
for(i=0;i<tlen;i++)
{
this.data[new String(tmparray)]=tmparray[++i]
}
this.QueryString=function(x)
{
return this.data[x]
}
}
Request = new QSHandler()

With this object, you could access the example variable by simply stating:

myvariable = Request.QueryString(&quot;variabletopass&quot;) jared@eae.net -
 
Is it possible to pass variables from one html form to another using javascript without having the variables displayed in the URL window? I'd prefer not to use cookies.
I look forward to any suggestions.

mowunmi
 
I don't know of any other way of doing it without using cookies - but then if you have some good cookie code they're not too much of a problem to use.

I've got some useful code in:

- I keep track of which pages have been accessed, and when the last visit was - so that I can have a page which is built to show pages not yet visited, or changed since the last visit (I don't get to see this information - it's local to the user) - see it in use on:
The code's pretty simple but comprehensive.

Do let me know if you use it/find it useful

stephen@daisy.fslife.co.uk
 
i m passing variables through pages using window.open method
p.e.
iwin=window.open(pagelocation, param ,&quot;status=no, scrollbars=yes,resizable=no,toolbar=no, location=no,menu=no,width=550,height=500&quot;);

param is a variable that holds the name of the window which will be opened.

then if you read from the 2d page the document.title property you get the value

If you want to see it in action visit
or
i use this trick to pass image names as a parameter in a page and display it there
 
That is very, very sneaky....and I like it! I think I'll have a play - as I've been looking for another way to pass simple data.

Cookies are great for complex stuff.

S
 
There's always the ol' hidden frame trick ;-). Just run everything inside a frameset in which the main frame takes up 100% of the space, and the frame that holds the variables stays hidden, using up at most 1 pixel at the top or bottom of your window. Your main frame will contain the forms, and will allow the user to proceed from form to form, while the hidden frame will stay the same, and will provide a &quot;container&quot; space to hold your variables.
 
Once again proving TMTOWTDI (There's More Than One Way To Do It). Those are all great suggestions! Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
rycamor,

It would be appreciated if you could give some examples for the &quot;ol' hidden frame trick&quot;.

Dove
 
OK, Let's just take the simplest example. You only need 2 elements:

1. Overall frameset that holds the individual elements.

2. One visible frame within that frameset that holds the form and elements where the user interacts.

But, I use 3 files to show the variable persistence:

1. index.html
Code:
<html>
<head>
<title>Variable Persistence Explained</title>
<script language=&quot;Javascript&quot;>

mainvar = &quot;Hello, World!&quot;;

</script>
</head>
<frameset cols=&quot;*&quot; rows=&quot;100%&quot;>
    <frame src=&quot;main.html&quot; name=&quot;main&quot; frameborder=&quot;0&quot; />
</frameset>
</html>

2. main.html
Code:
<html>
<head>
<title>First page</title>
</head>
<body>

Parent frame says:

<script language=&quot;Javascript&quot;>

//document.write(&quot;Hello World&quot;);
document.write(parent.mainvar);

</script>
<form name=&quot;theform&quot;>

Your Answer: <input type=&quot;text&quot; name=&quot;answer&quot;>
<input type=&quot;button&quot; name=submit value=&quot;Send&quot; onclick=&quot;parent.mainvar += '<br>' + document.title + ' says: ' +  document.theform.answer.value&quot;>
<br><br>
<input type=&quot;button&quot; name=next value=&quot;Go to next page: &quot; onclick=&quot;window.location = 'main2.html'&quot;>

</form>
</body>
</html>

3. main2.html
Code:
<html>
<head>
<title>Second page</title>
</head>
<body>

Parent frame says:

<script language=&quot;Javascript&quot;>

//document.write(&quot;Hello World&quot;);
document.write(parent.mainvar);

</script>
<form name=&quot;theform&quot;>

Your Answer: <input type=&quot;text&quot; name=&quot;answer&quot;>
<input type=&quot;button&quot; name=submit value=&quot;Send&quot; onclick=&quot;parent.mainvar += '<br>' + document.title + ' says: ' + document.theform.answer.value&quot;>
<br><br>
<input type=&quot;button&quot; name=next value=&quot;Go to next page: &quot; onclick=&quot;window.location = 'main.html'&quot;>

</form>
</body>
</html>

So, just place these three pages in the same folder, and load index.html in your browser. Enter an answer phrase to the &quot;Hello World!&quot; from the parent frameset. Then click the bottom button which loads the second page inside the frameset (main2.html). You will see that the parent frameset stores the variable update from the first, and outputs it to the page. You then add to it from this page, and it will be stored again, when you proceed to the first page again, etc...

You see the possibilities, I hope ;-). Now, this method doesn't realy use a &quot;hidden&quot; frame. It just stores the variable in the parent frameset. You can instead choose to have another frame:
Code:
<frameset cols=&quot;*&quot; rows=&quot;100%&quot;,&quot;1&quot;>
    <frame src=&quot;main.html&quot; name=&quot;main&quot; frameborder=&quot;0&quot; />
    <frame src=&quot;hidden.html&quot; name=&quot;hidden&quot; />
</frameset>

With this method, you can store variables in that frame, even submit them back to the server, and receive response data from the server, without refreshing the main page. This will take a little more work than I have time for right now, to make an example. I leave this as an exercise for the reader.

-------------------------------------------

&quot;Calculus is just the meaningless manipulation of higher symbols&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-unknown F student
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top