url parsing

F

Fabian

I want to be able to open a window with an url that has parameters like
so:

<a href="foo.html?xx=5&yy=6&ff=1&level=0">..</a>

And then javascript will enter these paramters as global variables.
However, if one or more of these variables are not set, it should use
default values for these variables, read from the start of the
javascript file. How do I parse and set these variables?
 
F

Fox

Fabian said:
I want to be able to open a window with an url that has parameters like
so:

<a href="foo.html?xx=5&yy=6&ff=1&level=0">..</a>

And then javascript will enter these paramters as global variables.
However, if one or more of these variables are not set, it should use
default values for these variables, read from the start of the
javascript file. How do I parse and set these variables?

since you want global defaults, and to cover the possibility that some
data may be missing from the url, declare the variables beforehand:

var xx = 0, yy=0, ff=0, level=0;


window.location.search will have everything after and including the "?".

var srch = window.location.search.substring(1); // cuts off the question mark

then split srch at the ampersand:

var parts = srch.split("&");

for(var i in parts)
{
var temp = parts.split("=");

window[temp[0]] = temp[1];
}


or you could simply use eval(parts); (depending on how you feel about
using eval) [*this would be equivalent to eval("xx=5"), for example].


You don't really need to initialize the globals, you can "collect" them
at runtime and test for their existance before using a default value, as in:

if(!someVar) someVar = initialValue;

etc...

there are also a number of other ways to do this... serverside solutions
are pretty cool (they'll allow you to use POST without showing any data
in the URL, and it supports much greater amounts of data).
 
F

Fabian

Fox hu kiteb:
window.location.search will have everything after and including the
"?".

var srch = window.location.search.substring(1);
// cuts off the question mark

// then split srch at the ampersand:

var parts = srch.split("&");
for(var i in parts) {
var temp = parts.split("=");
window[temp[0]] = temp[1];
}


or you could simply use eval(parts); (depending on how you feel
about using eval) [*this would be equivalent to eval("xx=5"), for
example].


Thats pretty much what I had, except it was not working, despite my
alerts telling me that the variables had been set with the intended
values. My using those variables later was coming up with all manner of
errors. Then I had an inspiration...

window[temp[0]] = 1 * temp[1];

I realised javascript doesnt really know teh difference between text and
numbers unless you tell it. So I told it. Now, it works. But your
producing the same (more or less) code I had did tell me that I was
looking in the wrong place for the problem. Thanks.
 
F

Fabian

Fabian hu kiteb:
window[temp[0]] = 1 * temp[1];

Espirit d'escalier:

if (temp[0] == "xx") { xx = 1 * temp[1]; }

Instead of the line I wrote earlier. That should stop any enterprising
hacker from writing random nonsense into the script.
 
F

Fox

Fabian said:
Fox hu kiteb:
window.location.search will have everything after and including the
"?".

var srch = window.location.search.substring(1);
// cuts off the question mark

// then split srch at the ampersand:

var parts = srch.split("&");
for(var i in parts) {
var temp = parts.split("=");
window[temp[0]] = temp[1];
}


or you could simply use eval(parts); (depending on how you feel
about using eval) [*this would be equivalent to eval("xx=5"), for
example].


Thats pretty much what I had, except it was not working, despite my
alerts telling me that the variables had been set with the intended
values. My using those variables later was coming up with all manner of
errors. Then I had an inspiration...

window[temp[0]] = 1 * temp[1];


oops -- you're right -- but only if you require number types... with
mixed types, this gets a little dicey, so you should probably test isNaN
too (if your data is mixed).

you might find that window[temp[0]] = +temp[1]; a little more efficient
-- it saves from having to execute a multiply (note that there is no
space between the '+' operator and the variable).
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
you might find that window[temp[0]] = +temp[1]; a little more efficient
-- it saves from having to execute a multiply (note that there is no
space between the '+' operator and the variable).

The absence of a space is unnecessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top