Asked  7 Months ago    Answers:  5   Viewed   36 times

Is there any JavaScript library that makes a dictionary out of the query string, ASP.NET style?

Something which can be used like:

var query = window.location.querystring["query"]?

Is "query string" called something else outside the .NET realm? Why isn't location.search broken into a key/value collection ?

EDIT: I have written my own function, but does any major JavaScript library do this?

 Answers

83

Maybe http://plugins.jquery.com/query-object/?

This is the fork of it https://github.com/sousk/jquery.parsequery#readme.

Tuesday, June 1, 2021
 
MGP
answered 7 Months ago
MGP
12

like this?

serialize = function(obj) {
  var str = [];
  for (var p in obj)
    if (obj.hasOwnProperty(p)) {
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
    }
  return str.join("&");
}

console.log(serialize({
  foo: "hi there",
  bar: "100%"
}));
// foo=hi%20there&bar=100%25

Edit: this one also converts recursive objects (using php "array" notation for the query string)

serialize = function(obj, prefix) {
  var str = [],
    p;
  for (p in obj) {
    if (obj.hasOwnProperty(p)) {
      var k = prefix ? prefix + "[" + p + "]" : p,
        v = obj[p];
      str.push((v !== null && typeof v === "object") ?
        serialize(v, k) :
        encodeURIComponent(k) + "=" + encodeURIComponent(v));
    }
  }
  return str.join("&");
}

console.log(serialize({
  foo: "hi there",
  bar: {
    blah: 123,
    quux: [1, 2, 3]
  }
}));
// foo=hi%20there&bar%5Bblah%5D=123&bar%5Bquux%5D%5B0%5D=1&bar%5Bquux%5D%5B1%5D=2&bar%5Bquux%5D%5B2%5D=3
Tuesday, June 1, 2021
 
Pradip
answered 7 Months ago
62

If you are just looking to get the values from the query string I use the following function:

function getQuerystring(key)
{
  key = key.replace(/[[]/,"\[").replace(/[]]/,"\]");
  var regex = new RegExp("[\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

Simply pass in the key you are looking for and get the value back. IE: getQueryString('upgraded') would return true

Thursday, August 26, 2021
 
dirigibleplum
answered 3 Months ago
89

You can write a wrapper component, that will switch what to render based on the query parameter

<Router history={browserHistory}>
   <Route path="abc/login.do" component={LoginComponent}/>
   <Route path="abc/publicLoginID.do" component={WrapperComponent} />
   <Route path="*" component={LoginComponent}/>
</Router>

//WrapperComponent

WrapperComponent = (props) => {
   if (props.location.query.phase==="def"){return <VerifyComponent {...props}/>}
   if (props.location.query.phase==="ghi"){return <ImageComponent {...props}/>}
}
Sunday, October 17, 2021
 
steros
answered 2 Months ago
18

Url property of the Request gets decoded in an internal method called CollapsePercentUFromStringInternal.

You can see this in the reflector. This I assumed is the default behaviour anyway.

Update

You can use RawUrl property to get hold of the un-decoded URL.

Saturday, November 27, 2021
 
DukeOfMarmalade
answered 4 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share