473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Referencing name/values in URL string

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
Jul 23 '05 #1
7 2357
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[i].split('=');
param.add(unescape(pair[0]), unescape(pair[1]));
}
}
Jul 23 '05 #2
"Jim Adamson" <ja********@go.com> wrote in message
news:c6**************************@posting.google.c 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>
Jul 23 '05 #3
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

Jul 23 '05 #4
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[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
}
}
Jul 23 '05 #5
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

Jul 23 '05 #6
<ja********@go.com> wrote in message
news:11**********************@z14g2000cwz.googlegr 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];
}
}
Jul 23 '05 #7
McKirahan

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

Jim

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: mplogue | last post by:
I have a form (frmMain) with a subform (frmSub), each with enumerated fields of the same name (txt1, txt2, etc). I'm trying to make a function that will take the values for each field in frmMain,...
5
by: Amelyan | last post by:
I am struggling here trying to determine what is a good programming practice as far as referencing your URLs. When you use Response.Redirect, do you use 1) Hard-coded string --...
0
by: Doug Gault | last post by:
I've been very pleased to find that you can load an XML file into a DATASET using the XMLREAD method, but I'm having a problem when trying to load a file that contains self-referencing elements. ...
17
by: Paul Helmuth | last post by:
All, (here's an easy one)... This is probably a stupid question - please bare with me as I am new to dotNet. How does one reference objects on a form from a module? In 6.0 you could simply...
2
by: HankD | last post by:
Hi, I am having a problem with instantiating two custom objects so they DO NOT point to the same memory location. What is happening is that changes I am making to my object1 are changing object2. I...
6
by: George Slamowitz | last post by:
Hello all I have a HTML control generated by the following: INPUT TYPE="hidden" NAME="MyAnswer" VALUE="" Is there a way to reference the Value of this control using VB program code??? ...
0
by: sansie | last post by:
Hi, I have a page which is basically a details view(in inset mode) inside a datalist. The problem is I want to set the field "invoiceID" (of the detailsview) to an invoiceID coming through the...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
4
blazedaces
by: blazedaces | last post by:
Hey guys, I'll try and show a basic program and tell you what I wish it could spit out, and then maybe you'll understand what I want to do, since it's hard to explain. Here's a simple public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.