Last active
March 16, 2021 04:52
-
-
Save iOnline247/0e3a4d781b4bb42d0058ded936691669 to your computer and use it in GitHub Desktop.
Get QueryStrings from URL (default) or string
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
| function getQueryStrings(v) { | |
| const result = {}; | |
| const rstripLeadingQuery = /^(:?\?|#)/; | |
| const queryString = (v ? String(v) : window.location.search).replace( | |
| rstripLeadingQuery, | |
| '' | |
| ); | |
| const re = /([^&=]+)=([^&]*)/g; | |
| let m; | |
| // eslint-disable-next-line no-cond-assign | |
| while ((m = re.exec(queryString))) { | |
| result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]); | |
| } | |
| return result; | |
| } | |
| // Usage: | |
| // const queryStrings = getQueryStrings(); | |
| // const queryStrings = getQueryStrings('?Id=23829&lcid=1033&token=easterEgg'); | |
| // const queryStrings = getQueryStrings('Id=23829&lcid=1033&token=easterEgg'); // Missing the leading `?` | |
| // const queryStrings = getQueryStrings('#here=nope'); // parses hashes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment