Skip to content

Instantly share code, notes, and snippets.

@janv
Created March 21, 2012 18:00
Show Gist options
  • Select an option

  • Save janv/2150348 to your computer and use it in GitHub Desktop.

Select an option

Save janv/2150348 to your computer and use it in GitHub Desktop.
Save Query parameters to location.href
save_state = (stateInfo) ->
currentState = {}
for term in window.location.href.split(/\?|&/)
[key, value] = term.split('=')
continue unless value?
currentState[key] = decodeURIComponent(value)
for key, value of stateInfo
if value?
currentState[key] = value
else
delete currentState[key]
queryString = ("#{key}=#{encodeURIComponent(value)}" for key, value of currentState).join('&')
history.pushState(({}), document.title, "#{window.location.pathname}?#{queryString}")
currentState
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment