#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Ireland
    Posts
    91
    Rep Power
    14

    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
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    24
    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!
  4. #3
  5. No Profile Picture
    Just Diggity
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Location
    Sherman Oaks, CA
    Posts
    126
    Rep Power
    13
    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);
    Last edited by diggity; November 29th, 2001 at 08:07 PM.
    Dave Pedowitz
  6. #4
  7. No Profile Picture
    #!/usr/beer/hurl
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Toronto
    Posts
    46
    Rep Power
    14
    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?
  8. #5
  9. No Profile Picture
    Just Diggity
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Location
    Sherman Oaks, CA
    Posts
    126
    Rep Power
    13
    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
    Dave Pedowitz

IMN logo majestic logo threadwatch logo seochat tools logo