You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Hi Sven,

    Thanks - I do show URL being used in the examples for a web server, but the current solution isn't great. I've just found that in node you can do url.parse(req.url,true) which should give you all the info you need (http://nodejs.org/docs/latest/api/url.ht­ml). While Espruino has this implemented, it doesn't parse the query string, which is what you need. I've created a bug for this and hopefully I'll get around to fixing it soon.

    In the mean time, you can try something like this:

    function parseURL(url) {
      var idx = url.indexOf("?");
      var map = {};
      if (idx>=0) {
        var args = url.substr(idx+1).split("&");
        for (var n in args) {
          var arg = args[n];
          var eq = arg.indexOf("=");
          if (eq<0) eg=arg.length;
          map[arg.substr(0,eq)] = arg.substr(eq+1);
        }
      }
      return map;
    }
    
    parseURL("/hello?key=val&foo=bar&somethi­ng");
    // ={"key":"val","foo":"bar","something":''­}
    
About

Avatar for Gordon @Gordon started