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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting variable with query strings

Sending data between pages

setting variable with query strings

by  GIGN  Posted    (Edited  )
We can send information to another page thru the use of a query string. It goes on the end of the URL when we give the href to a link:

<a href="nextPage.htm[color red]?some variable=value[/color]>"
The ? sign indicates the start of the query string
we can have as many variable/value pairs as we like, we separate them using the & sign:

...[color red]?someVariable=value&someOther=value2...[/color]

When the next page is loaded the query string is stored for us at [color red] location.search [/color].When it arrives the one above would look like this:

[color red]?someVariable=value&someOther=value2[/color]

which is one big string,
[color green]Note:[/color] If any spaces are left in the pairs, they come with %20 inserted instead.
some variable --> some%20variable
now we have to slice it up so we can use it. You can do many things, you could store them in a matrix, or just set the variables using eval (simpler).
[color red]
function getQuery(){
var queryString = location.search;

-- get rid of ? at start --
var data = queryString.slice(1,data_pre.length);


-- Splits the string at the & and returns an array containing the parts, variable=value pairs.Stored as single string. --

dataArray = data.split("&");

-- Here is where you could take each element of the dataArray and split at the = sign, and store the variable values separately. I am just going to set the variables. --

for (j=0;j<dataArray.length;j++){
eval(dataArray[j])
}

}
[/color]

So the first entry for our dataArray looked like:
someVariable=value

using eval(someVariable=value) sets the variable with it's value.

While sometimes these techniques are useful - a middleware solution is far les time consuming to set up. Server-Side pre-processors like ASP,PHP,PERL etc are ideally suited to this kind if job, and have pre defined methods making all of this unnecessary.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top