Last active
March 8, 2019 10:21
-
-
Save tripulse/09217ae0e7abe7a5242fa1b032f8be7b to your computer and use it in GitHub Desktop.
Parses QueryString and returns URL agruments.
This file contains 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 GetQueryString( qs ) { | |
if ( !qs ) return; | |
var QueryStrings = []; | |
var SearchParams = new URLSearchParams( qs ); | |
for ( Query of SearchParams ) { | |
QueryStrings.push( Query ); | |
} | |
function EntriesToObject( Entries ) { | |
if ( Array.isArray( Entries ) ) { | |
var EntriesObject = {}; | |
Entries.forEach( entry => { | |
EntriesObject[ entry[ 0 ] ] = entry[ 1 ]; | |
} ); | |
return EntriesObject; | |
} | |
} | |
QueryStrings = EntriesToObject( QueryStrings ); | |
} |
This also could be done using:
var searchParams =
new URL(location.href)
.searchParams;
// This is how you do it!
var params =
Array.from(searchParams.entries())
.reduce((accum, [k, v]) => {
accum[k] = v;
return accum;
}, {});
Thanks to this StackOverflow answer.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Takes
qs
argument as QueryString. Otherwise returns nothing.