471,316 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

parse and display parts of query string

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

Jul 23 '05 #1
5 1899
Jay

"Scott" <sm*****@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hello,

I have to admit, I'm not very proficient with javascript, and I'm
hoping someone in this group can help.
Does this help?
http://support.internetconnection.ne...y_String.shtml

Jay
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

Jul 23 '05 #2
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

Jul 23 '05 #3
Scott wrote:
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[i];
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>

Jul 23 '05 #4
Scott wrote:
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).replac e(/&/g,"<BR>"));

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

Jul 23 '05 #5
Scott wrote:
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
<b0************@ID-110599.news.dfncis.de>
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Polar | last post: by
1 post views Thread by fowlertrainer | last post: by
29 posts views Thread by gs | last post: by
AdrianH
5 posts views Thread by AdrianH | last post: by
AdrianH
1 post views Thread by AdrianH | last post: by
5 posts views Thread by meendar | last post: by
2 posts views Thread by Mufasa | last post: by
reply views Thread by rosydwin | last post: by

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.