4

In order to perform AJAX calls, I extract URL values (embedded IDs and query string parameters) and store them in hidden inputs. Outside of this little bit of server-side work, I have a static HTML page.

I am looking for a library that can easily extract my URL values on the client-side. I'd like to specify the expected format (a URL template if you will): /account/{accountId}/edit?param={param}. Then I'd like the library to create an object based on the actual URL (based on window.location): /account/123/edit?param=234 and give me back an object: { accountId: 123, param: 234 }.

It would be nice if the library would allow me to indicate the expected type {accountId:int} so that the resulting object has a true, int, float, date, etc.

Does such a library exist for client-side JavaScript?

Travis Parks
  • 8,435
  • 12
  • 52
  • 85
  • I'm actually thinking about looking at Express' routing classes: https://github.com/visionmedia/express/blob/master/lib/router/route.js. With that at its core, I think I could pretty easily create my own project. In addition, I'd probably make it easy to do the reverse, where you supply a template and an object and construct a URL with it. – Travis Parks Jan 15 '14 at 02:27
  • I tried using Express' `Route` class but it was including the `?` if the query string was right up against a path variable. I am now looking at node.js' `url.js` and `querystring.js` for parsing URLs. I have the code to extract the URL parts and find placeholders, but I am not sure how to do the extraction piece quite yet. – Travis Parks Jan 18 '14 at 16:17
  • 1
    I was able to implement an initial version of this library using Node's `url.js` file. It allows values to be extracted from a URL path and query string. It also allows applying values to a URL template to create a URL. I will be creating a GitHub project soon so I can share it. – Travis Parks Jan 18 '14 at 22:24
  • 1
    Here is the GitHub repository: https://github.com/jehugaleahsa/earl.js – Travis Parks Jan 19 '14 at 00:18
  • If you are using AngularJS, the $resource object can handle a lot of this. – Travis Parks Sep 11 '14 at 13:34
  • Thats very good ! i am using it too. – Phyo Arkar Lwin May 17 '15 at 12:50

0 Answers0