Parse a query string into an object


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var uri = window.location.href; //http://www.yoururl.com?id=test
  2. var queryString = {};
  3. uri.replace(
  4. new RegExp("([^?=&]+)(=([^&]*))?", "g"),
  5. function($0, $1, $2, $3) { queryString[$1] = $3; }
  6. );
  7. console.log(queryString['id']); //returns 'test' or 'undefined' if not found

URL: http://stevenbenner.com/2010/03/javascript-regex-trick-parse-a-query-string-into-an-object/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.