Skip to Content

Access GET Parameters With Coffeescript

Posted on One min read

I’ve been working on a pure javascript based search engine for this static website and needed to access a get parameter within the URL.

I found a few solutions online but they usually made use of jQuery or weren’t in coffeescript. A few others would only extract an individual named parameter at a time. The following will return all of them in Javascript’s equilvalent of a hash (or dictionary if you prefer) in the form of an object.

getParams = ->
  query =
  raw_vars = query.split("&")

  params = {}

  for v in raw_vars
    [key, val] = v.split("=")
    params[key] = decodeURIComponent(val)



If compiled and included in a page the above will print out the parameters as a hash object to the console.