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.

Revisions

  1. jeandcr created this gist Nov 16, 2018.
    15 changes: 15 additions & 0 deletions query_string.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    /**
    *
    * 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, " "));
    }