Skip to content

Instantly share code, notes, and snippets.

@jeandcr
Created November 16, 2018 16:09
Show Gist options
  • Select an option

  • Save jeandcr/e6adf084a3970a92e3a3c834a48d03e1 to your computer and use it in GitHub Desktop.

Select an option

Save jeandcr/e6adf084a3970a92e3a3c834a48d03e1 to your computer and use it in GitHub Desktop.
Return a URL query string by key.
/**
*
* Returns a URL query parameter by it's key.
* eg. You have a URL: http://host.com/some/path?uniquekey=value
* The function call qs('uniquekey') returns 'value'
*
* @param key string
* @return string
*
*/
function qs(key) {
key = key.replace(/[*+?^$.\[\]{}()|\\\/]/g, "\\$&"); // escape RegEx meta chars
var match = location.search.match(new RegExp("[?&]"+key+"=([^&]+)(&|$)"));
return match && decodeURIComponent(match[1].replace(/\+/g, " "));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment