JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 23rd, 2001, 06:46 AM
chakotha chakotha is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Ireland
Posts: 91 chakotha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old November 23rd, 2001, 07:07 AM
binky's Avatar
binky binky is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2001
Location: New Zealand
Posts: 1,774 binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 58 m 15 sec
Reputation Power: 23
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!

Reply With Quote
  #3  
Old November 29th, 2001, 07:08 PM
diggity diggity is offline
Just Diggity
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: Sherman Oaks, CA
Posts: 126 diggity User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old November 6th, 2003, 12:17 PM
cygnus cygnus is offline
#!/usr/beer/hurl
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Location: Toronto
Posts: 46 cygnus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #5  
Old November 6th, 2003, 02:22 PM
diggity diggity is offline
Just Diggity
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: Sherman Oaks, CA
Posts: 126 diggity User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Accessing query string variables with Javascript

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap