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

Variables in the url 1

Status
Not open for further replies.

seanbo

Programmer
Jun 6, 2003
407
0
0
GB
i want to be able to pass variables within the url. how can i do this?

____________________________________________________
If you like a post, show you care by giving it a <censored>.
 
Append to the URL:

XvariableName=variableValue

...for each variable desired. The 'X' should be replaced by a '?' for the first variable, and an '&' for each subsequent variable.

For example:

this.location = this.location.href + "?name=personnel&date=today&showall=false";

Good luck.

--Dave
 
it's actually reading the variables at the other end that i'm having difficulty with.

____________________________________________________
If you like a post, show you care by giving it a <censored>.
 
At the other end:

Code:
var params = this.location.search.substring(1).split('&');
for(var i=0; i<params.length; i++)
 alert(params[i]);

Of course, you won't 'alert' the params, you'll do something else with them, but this will prove to you that you caught them in an array.

The substring(1) slices off the '?' from the beginning, then the split('&') divides up the list, creating new array indeces each time it encounters an '&'.

Note, however, that a peculiarity of javascript seems (in IE6, anyway) to capture at least one BLANK param if there is no parameter list. Therefore, you might need a little check like this:

Code:
var params = this.location.search.substring(1).split('&');
for(var i=0; i<params.length; i++)
 [b]if(params[i] != "")[/b]
  alert(params[i]);

Good luck!

--Dave
 
or if you're using a server side script like ASP you could do something like this:

the address:

the code:
a = Request.Querystring(a);
b = Request.Querystring(b);

-kaht

banghead.gif
 
lfi, you method seems to want the variables writing without their variable names, as outlined in your first post, but that's just fine. thanks for the help. i may choose to keep the variable names for verification, then take the operand using split - but actually, a list of operands nly is more akin to my lazy style of programming.

kaht, cheers. i'm sure that information will come in usefull at some point in time.

____________________________________________________
If you like a post, show you care by giving it a <censored>.
 
NEW:

how can i pass a carriage space in the url? i've tried passing it as %20, but to no avail. is there something along the lines of \n i can use - but for spaces, not returns?

____________________________________________________
If you like a post, show you care by giving it a <censored>.
 
You can do one of two things. If you don't mind the potential hassles that quotes bring (specifically when you start nesting quotes within quotes), you can simply use quotes around the parameter value in the URL as such:

Code:
<html>
<head>
<script>
function redrawMe()
{
 var currLoc = this.location.href;
 if(currLoc.indexOf('?') > -1)
  currLoc = currLoc.substring(0, currLoc.indexOf('?'));

 this.location = currLoc + "?name=[b]'Dog Walker'[/b]";
}

function showParam()
{
 var params = this.location.search.substring(1).split('&');
 for(var i=0; i<params.length; i++)
  if(params[i] != "")
   alert([b]unescape[/b](params[i]));
}
</script>
</head>
<body onload='showParam();'>
<input type='button' value='redraw' onclick='redrawMe()' />
</body>
</html>

I did that with IE6. The received value shows up as '%20' where the space is. The unescape replaces '%20' with the space.

Another thing you can do is replace spaces in the outgoing code with a symbol you are certain would NOT appear in the URL otherwise. Underscores (_) typically work well for this. Then, on the receiving end, replace the underscores with spaces again.

Good luck!

--Dave
 
all good.

____________________________________________________
If you like a post, show you care by giving it a <censored>.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top