Skip to content

Instantly share code, notes, and snippets.

@rc1021
Created May 13, 2019 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rc1021/3703bb284e05d97faf5e0fec834c733d to your computer and use it in GitHub Desktop.
Save rc1021/3703bb284e05d97faf5e0fec834c733d to your computer and use it in GitHub Desktop.
function queryStringToJson(s="")
{
return JSON.parse(s.replace(/[?|&]([A-Za-z0-9]*)=([^&]*)/ig, ",\"$1\":\"$2\"").slice(1).replace(/(.*)/, "{ $1 }"));
}
// url like https://www.your.domain/pathname?p=2&act=delete&type=photo&id=0001
var queryObject = queryStringToJson(location.search);
console.log(queryObject);
// result:
// {p: "2", act: "delete", type: "photo", id: "0001"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment