Referencing name/values in URL string

J

Jim Adamson

I have created a web page that receives names and values from a URL
string of another page e.g.
http://hostname/resolve?sublibrary=JMLibrary&collection=Elton&shelfmark=LM
36TY
... and decodes the names/values from the ? onwards, doing all the
seperation of the ampersands.

Here is the "decoder" that I found at
http://www.tek-tips.com/faqs.cfm?fid=5442

==========================================
function getValue(varname)
{
// First, we load the URL into a variable
var url = window.location.href;

// Next, split the url by the ?
var qparts = url.split("?");

// Check that there is a querystring, return "" if not
if (qparts.length == 0)
{
return "";
}

// Then find the querystring, everything after the ?
var query = qparts[1];

// Split the query string into variables (separates by &s)
var vars = query.split("&");

// Initialize the value with "" as default
var value = "";

// Iterate through vars, checking each one for varname
for (i=0;i<vars.length;i++)
{
// Split the variable by =, which splits name and value
var parts = vars.split("=");

// Check if the correct variable
if (parts[0] == varname)
{
// Load value into variable
value = parts[1];

// End the loop
break;
}
}

// Convert escape code
value = unescape(value);

// Convert "+"s to " "s
value.replace(/\+/g," ");

// Return the value
return value;
}

// end hide -->
</script>
==============================================


I would like to know how to make name/value pairs available to an "if,
then, else statement"
i.e. how i could expand the getValue function so that it would accept
something like:

if(collection == 'Elton')

....do such and such e.g. open a pop-up window.

I suppose what I am asking is how do I reference these name/value
pairs so I can do something with them.

thanks a lot
Jim
 
V

VK

I suppose what I am asking is how do I reference these name/value
pairs so I can do something with them.

Here we go, Virginias, who still think that THERE IS A HASH (associative
array) in JavaScript, try to help the guy. No keys - no access to values!

To Mr. Adamson:

You should use my freshly made PGH (Pretty Good Hash), see the posting named
"PGH : Pretty Good Hash v0.1"

Also the key/value processing block could be much shorter:

var param = new Hash();
....
function getParameters() {
var tmp = self.location.search.split('&');
var pair;
for (i=0;tmp.length;i++) {
pair = tmp.split('=');
param.add(unescape(pair[0]), unescape(pair[1]));
}
}
 
M

McKirahan

Jim Adamson said:
I have created a web page that receives names and values from a URL
string of another page e.g.
http://hostname/resolve?sublibrary=JMLibrary&collection=Elton&shelfmark=LM
36TY
.. and decodes the names/values from the ? onwards, doing all the
seperation of the ampersands.

Here is the "decoder" that I found at
http://www.tek-tips.com/faqs.cfm?fid=5442

==========================================
function getValue(varname)
{
// First, we load the URL into a variable
var url = window.location.href;

// Next, split the url by the ?
var qparts = url.split("?");

// Check that there is a querystring, return "" if not
if (qparts.length == 0)
{
return "";
}

// Then find the querystring, everything after the ?
var query = qparts[1];

// Split the query string into variables (separates by &s)
var vars = query.split("&");

// Initialize the value with "" as default
var value = "";

// Iterate through vars, checking each one for varname
for (i=0;i<vars.length;i++)
{
// Split the variable by =, which splits name and value
var parts = vars.split("=");

// Check if the correct variable
if (parts[0] == varname)
{
// Load value into variable
value = parts[1];

// End the loop
break;
}
}

// Convert escape code
value = unescape(value);

// Convert "+"s to " "s
value.replace(/\+/g," ");

// Return the value
return value;
}

// end hide -->
</script>
==============================================


I would like to know how to make name/value pairs available to an "if,
then, else statement"
i.e. how i could expand the getValue function so that it would accept
something like:

if(collection == 'Elton')

...do such and such e.g. open a pop-up window.

I suppose what I am asking is how do I reference these name/value
pairs so I can do something with them.

thanks a lot
Jim


Not sure if this will help:

http://localhost/pairs.htm?ID=123

<html>
<head>
<title>pairs.htm</title>
<script type="text/javascript">
var nam = new Array();
var val = new Array();
var xqs = location.search;
xqs = xqs.replace(/\?/g,"&");
var xnv = xqs.split("&");
for (var i=1; i<xnv.length; i++) {
var xxx = xnv.split("=");
nam[i-1] = xxx[0];
val[i-1] = xxx[1];
}
for (var j=0; j<nam.length; j++) {
if (nam[j] == "ID") alert("ID = " + val[j]);
}
</script>
</head>
<body>
</body>
</html>
 
C

codeHanger

VK said:
Here we go, Virginias, who still think that THERE IS A HASH (associative
array) in JavaScript, try to help the guy. No keys - no access to values!

Well, Petunia, it appears to me that you also have "No keys - no access
to values!" either, until after you've generated them.

Oh, wait a minute, I see what you're saying -- you do:

alert( param.getValue( "constructor" ))

==> function Object() {
[native code]
}
To Mr. Adamson:

You should use my freshly made PGH (Pretty Good Hash), see the posting named
"PGH : Pretty Good Hash v0.1"

Perhaps Mr. Adamson should. But only after he's been provided with
appropriate quailfication on the use of PGH?

... /rh
 
V

VK

Perhaps Mr. Adamson should. But only after he's been provided with
appropriate quailfication on the use of PGH?

Well, I have a Christmas special this year: free PGH qualification :)

Can be done without PGH also, as this particular case doesn't involve real
hash manipulations (sorting, key/value change/delete etc.)

var keys = new Array();
var values = new Array();
....
function getParameters() {
var tmp = self.location.search.split('&');
var pair;
for (i=0;tmp.length;i++) {
pair = tmp.split('=');
keys.push(unescape(pair[0]));
values.push(unescape(pair[1]));
}
}
...
for (i=0;keys.length;i++) {
if (keys // meets your criteria) {
// use values in the way you want
}
}
 
J

jadamson60

McKirahan

Thankyou for your reply to my question. I have substituted the third
line of the javascript

var xqs = unescape(location.search);

Is this the best way to rid the %20's from the URL ?
I have another question if I may. If the URL is say:

http://localhost/pairs.htm?ID=123&name=john&street=Brook

....how do you adapt this for the if-then-else statement ? i.e. if ID is
equal to 123 AND name is equal to John AND street is equal to Brook,
alert ( Hello John ;)

This doesn't work:

if (nam[j] == "ID" && val[j]=="123" && nam[j] == "nam" &&
val[j]=="John" && nam[j] == "street" && val[j]=="Brook")

One last thing - if I know that the name is always going to be one of
three text strings (i.e. ID, Name and street),
would it be worth changing the script so that you're not always
querying:

if (nam[j] == "ID"

but rather if

("ID" =="123"

How could you change this ?

Many thanks in advance.

Jim
 
M

McKirahan

McKirahan

Thankyou for your reply to my question. I have substituted the third
line of the javascript

var xqs = unescape(location.search);

Is this the best way to rid the %20's from the URL ?
I have another question if I may. If the URL is say:

http://localhost/pairs.htm?ID=123&name=john&street=Brook

...how do you adapt this for the if-then-else statement ? i.e. if ID is
equal to 123 AND name is equal to John AND street is equal to Brook,
alert ( Hello John ;)

This doesn't work:

if (nam[j] == "ID" && val[j]=="123" && nam[j] == "nam" &&
val[j]=="John" && nam[j] == "street" && val[j]=="Brook")


1) Typo? nam[j] == "nam"; shouldn't it be: nam[j] == "name".

One last thing - if I know that the name is always going to be one of
three text strings (i.e. ID, Name and street),
would it be worth changing the script so that you're not always
querying:


2) Will it be "Name" (as above) or "name" (as in the URL)?

if (nam[j] == "ID"

but rather if

("ID" =="123"

How could you change this ?

Many thanks in advance.

Jim

Try assigning the value to a variable when it's found.

var xID = "";
var xNM = "";
var xST = "";
for (var j=0; j<nam.length; j++) {
var what = nam[j];
if (what == "ID") {
xID = val[j];
} else if (what == "name") {
xNM = val[j];
} else if (what == "street") {
xST = val[j];
}
}
if (xID == "123" && xNM == "John" && xST == "Brook") alert("!");


Might case-sensitivity be an issue?

Could the QueryString be:

http://localhost/pairs.htm?Id=123&Name=john&Street=Brook

or other variation (as suggested above)? If so, then use this:

for (var j=0; j<nam.length; j++) {
var what = nam[j].toLowerCase();
if (what == "id") {
xID = val[j];
} else if (what == "name") {
xNM = val[j];
} else if (what == "street") {
xST = val[j];
}
}
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top