The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Accessing query string variables with Javascript
Discuss Accessing query string variables with Javascript in the JavaScript Development forum on Dev Shed. Accessing query string variables with Javascript JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 23rd, 2001, 06:46 AM
|
|
Contributing User
|
|
Join Date: May 2001
Location: Ireland
Posts: 91
Time spent in forums: 43 m 37 sec
Reputation Power: 13
|
|
|
Accessing query string variables with Javascript
Hi,
I might be losing the plot here but can I get at the query string values in the address bar of the browser with Javascript client-side?
Thanks for any help
|

November 23rd, 2001, 07:07 AM
|
 |
Contributing User
|
|
Join Date: Oct 2001
Location: New Zealand
|
|
At first I thought the answer would be no, but then I had a thought and realised that you can. Not in the same way but it's possible. What you do is:
if you had a URL such as http://www.here.com/here.html?a=5&b=6
all you have to do is do a bit of javascript such as:
var urlString = unescape(window.location)
that gives you the url, then:
var a = urlString.substr(urlString.indexOf("a="), urlString.length)
and work from there!
|

November 29th, 2001, 07:08 PM
|
|
Just Diggity
|
|
Join Date: Nov 2001
Location: Sherman Oaks, CA
Posts: 126
Time spent in forums: < 1 sec
Reputation Power: 12
|
|
Also there is a location object that will return only the query string part (everything after .com/net etc including the ?) of the URL.
location.search
so using the same URL example: http://www.here.com/here.html?a=5&b=6
you would do something like:
var query = location.search;
var aValue = query.substring(query.indexOf("a=") + 2, query.indexOf("&"));
var bValue = query.substring(query.indexOf("b=") + 2, query.length);
__________________
Dave Pedowitz
Last edited by diggity : November 29th, 2001 at 08:07 PM.
|

November 6th, 2003, 12:17 PM
|
|
#!/usr/beer/hurl
|
|
Join Date: Aug 2001
Location: Toronto
Posts: 46
Time spent in forums: < 1 sec
Reputation Power: 12
|
|
|
This thread is an oldie!
Anyway, I'm trying to do something like this, and this script is close.
The only problem is that I don't know how long the value of a variable will be, so how do you leave out the specific query length?
|

November 6th, 2003, 02:22 PM
|
|
Just Diggity
|
|
Join Date: Nov 2001
Location: Sherman Oaks, CA
Posts: 126
Time spent in forums: < 1 sec
Reputation Power: 12
|
|
|
This thread is an oldie!!! I developed the following method for retrieving parameter values from the query string. But, you must know the parameter name in order to retrieve the value... Now, knowing the parameter name would leave me to believe that you would know the length, so this may not be appropriate for you, but here it is anyhow:
q = location.search;
getParam = function(arg) {
if (q.indexOf(arg) >= 0) {
var pntr = q.indexOf(arg) + arg.length + 1;
if (q.indexOf("&", pntr) >= 0) {
return q.substring(pntr, q.indexOf("&", pntr));
} else {
return q.substring(pntr, q.length);
}
} else {
return null;
}
}
So you'd end up with something like:
?foo=someValue
var val = getParam("foo");
and val would equal "someValue"
If you do not know the parameter names you may want to develop a function that parses the query string and returns a multi-dimensional of name / value pairs
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|