Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Referencing name/values in URL string

Reply
Thread Tools

Referencing name/values in URL string

 
 
Jim Adamson
Guest
Posts: n/a
 
      12-14-2004
I have created a web page that receives names and values from a URL
string of another page e.g.
http://hostname/resolve?sublibrary=J...n&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[i].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
 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      12-14-2004
> 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[i].split('=');
param.add(unescape(pair[0]), unescape(pair[1]));
}
}


 
Reply With Quote
 
 
 
 
McKirahan
Guest
Posts: n/a
 
      12-14-2004
"Jim Adamson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I have created a web page that receives names and values from a URL
> string of another page e.g.
> http://hostname/resolve?sublibrary=J...n&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[i].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[i].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>


 
Reply With Quote
 
codeHanger@yahoo.ca
Guest
Posts: n/a
 
      12-14-2004
VK wrote:
> > 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!
>


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

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      12-15-2004
> 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[i].split('=');
keys.push(unescape(pair[0]));
values.push(unescape(pair[1]));
}
}
...
for (i=0;keys.length;i++) {
if (keys[i] // meets your criteria) {
// use values[i] in the way you want
}
}


 
Reply With Quote
 
jadamson60@go.com
Guest
Posts: n/a
 
      12-15-2004
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&na...n&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

 
Reply With Quote
 
McKirahan
Guest
Posts: n/a
 
      12-15-2004
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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&na...n&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&Na...n&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];
}
}


 
Reply With Quote
 
jadamson60@go.com
Guest
Posts: n/a
 
      12-16-2004
McKirahan

This has done the trick ! Many thanks for your help.

Jim

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert url with query string to url without query string nick Javascript 1 02-13-2011 11:20 PM
What is the common practice as far as URL page referencing in your code? Amelyan ASP .Net 5 05-02-2005 11:22 PM
URL - substitution of a correct URL by a GUID like URL in favorites. Just D. ASP .Net Mobile 0 08-11-2004 04:26 PM
redirect URL's, return URL's, and URL Parameters Jon paugh ASP .Net 1 07-10-2004 05:29 AM
Javascript & URL referencing Marc HTML 0 06-24-2004 02:22 PM



Advertisments