Posted By

Pjotor on 04/23/13


Tagged

location search query json


Versions (?)

Search string to JSON


 / Published in: JavaScript
 

URL: http://pjotor.com

Since I'm doing this from time to time, I thought I'd put a snipplet here.

This function returns a JSON objectbased on document.location.search

  1. function searchToJSON(){
  2. var rep = {'?':'{"','=':'":"','&':'","'};
  3. var s = document.location.search.replace(
  4. /[\?\=\&]/g, function(r){ return rep[r]; }
  5. );
  6. return JSON.parse( s.length? s+'"}' : "{}" );
  7. }

Report this snippet  

You need to login to post a comment.