parse and display parts of query string

S

Scott

Hello,

I have to admit, I'm not very proficient with javascript, and I'm
hoping someone in this group can help.

What I have is some SHTML pages that I need to be able to dissect and
print parts of the query string. I'm using Apache SSI to display the
shtml pages, but as far as I can tell, SSI doesn't have any way to
split apart a variable like this, so that's why I'm turning to
javascript.

For example, let's save I have the following URL:

page.shtml?foo=1234&bar=5678

Can someone show me some simple javascript that I could insert inside
of page.shtml that would print the value of "foo" and "bar".

Also, I'd like to know how to take these values and potentially insert
them into some form fields.

Thank you for your help!
Scott
 
S

Scott

Hello Jay,

It almost worked. The page is actually loaded as the result of a CGI
script that uses a "Location:" header to cause a redirect.

Here's how it works:

1) user opens "/cgi-bin/script"

2) /cgi-bin/script responds with:
Location: /page.shtml?foo=1234&bar=1234

3) /page.shtml runs the javascript code, but the problem is the
variable this.location = "/cgi-bin/script" (not
"/page.shtml?foo=1234&bar=1234")

Any ideas?

Thanks,
Scott
 
R

RobB

Scott said:
Hello Jay,

It almost worked. The page is actually loaded as the result of a CGI
script that uses a "Location:" header to cause a redirect.

Here's how it works:

1) user opens "/cgi-bin/script"

2) /cgi-bin/script responds with:
Location: /page.shtml?foo=1234&bar=1234

3) /page.shtml runs the javascript code, but the problem is the
variable this.location = "/cgi-bin/script" (not
"/page.shtml?foo=1234&bar=1234")

Any ideas?

Thanks,
Scott

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">

p {
font: normal 13px "times new roman";
}
input {
width: 100px;
text-align: center;
}
</style>
<script type="text/javascript">

function getQval(name)
{
var m,
re = new RegExp('&?' + name + '=([^&]+)');
var Qstr = window.location.search.substring(1);
return (Qstr && (m = Qstr.match(re))) ?
unescape(m[1]) : '';
}

function qPopulate(f_id)
{
var f, el, elname, elval;
if ((f = document.forms[f_id])
|| document.getElementById
&& (f = document.getElementById(f_id)))
{
for (var i = 1, l = arguments.length; i < l; ++i)
{
elname = arguments;
if ((el = f.elements[elname])
&& 'undefined' == typeof el[0]
&& (elval = getQval(elname)))
{
el.value = elval;
}
}
}
}

window.onload = function()
{
qPopulate('f1', 'foo', 'bar', 'baz'); //(form name/id, [field names])
}

</script>
</head>
<body>
<p>
The value of "foo" is
<script type="text/javascript">
document.write('<strong> ' + getQval('foo') + '</strong>.');
</script>
</p><p>
The value of "bar" is
<script type="text/javascript">
document.write('<strong> ' + getQval('bar') + '</strong>.');
</script>
</p>
<form name="f1">
<input type="text" name="foo" value="">___foo
<br>
<input type="text" name="bar" value="">___bar
<br>
<input type="text" name="baz" value="">___baz
</form>
</body>
</html>
 
M

Mick White

Scott said:
Hello,

I have to admit, I'm not very proficient with javascript, and I'm
hoping someone in this group can help.

What I have is some SHTML pages that I need to be able to dissect and
print parts of the query string. I'm using Apache SSI to display the
shtml pages, but as far as I can tell, SSI doesn't have any way to
split apart a variable like this, so that's why I'm turning to
javascript.

For example, let's save I have the following URL:

page.shtml?foo=1234&bar=5678

Can someone show me some simple javascript that I could insert inside
of page.shtml that would print the value of "foo" and "bar".

document.write(location.search.substring(1).replace(/&/g said:
Also, I'd like to know how to take these values and potentially insert
them into some form fields.
v=location.search.substring(1).split("&")
x=v.length
while(x--){
window[v[x].split("=")[0]]=v[x].split("=")[1]
}

Mick

Thank you for your help!
Scott
 
T

Thomas 'PointedEars' Lahn

Scott said:
1) user opens "/cgi-bin/script"

2) /cgi-bin/script responds with:
Location: /page.shtml?foo=1234&bar=1234

3) /page.shtml runs the javascript code, but the problem is the
variable this.location = "/cgi-bin/script" (not
"/page.shtml?foo=1234&bar=1234")

Any ideas?

Location header value must be an absolute URI (see
RFC 1945, 10.11 and RFC 2616, 14.30), try that first.


PointedEars
--
Was muß ich ändern, damit es wieder funkt?
Eine größere Antenne holen?
-- Mario Müller in dcs.moz
<[email protected]>
 

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