Created
March 21, 2012 18:00
-
-
Save janv/2150348 to your computer and use it in GitHub Desktop.
Save Query parameters to location.href
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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