Last active
August 18, 2021 14:36
-
-
Save zeshanshani/5f19ecde450986e6314fc43a49495c67 to your computer and use it in GitHub Desktop.
These functions include inserting a URL parameter, getting value from a URL parameter, and parsing query string
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
/** | |
* Insert Parameter in URL | |
* | |
* Dynamically insert or update a parameter in the URL. | |
* | |
* @param {string} key parameter name | |
* @param {string} value parameter value | |
* @param {string} base set a custom URL base. | |
* @param {boolean} historyUpdate defines if it update the URL without reloading the page | |
*/ | |
const insertParamToUrl = function( key, value, base = '', historyUpdate = false ) { | |
// Check if both key exists. | |
var kvp = document.location.search.substr(1).split('&'); | |
if ( key ) { | |
key = encodeURI( key ); | |
value = value ? encodeURI( value ) : ''; | |
var i = kvp.length; var x; | |
while ( i-- ) { | |
x = kvp[i].split( '=' ); | |
if ( x[0] == key ) { | |
x[1] = value; | |
kvp[i] = x.join( '=' ); | |
break; | |
} | |
} | |
if ( i < 0 ) { | |
kvp[kvp.length] = [key, value].join('='); | |
} | |
} | |
// this will reload the page, it's likely better to store this until finished | |
// document.location.search = kvp.join( '&' ); | |
var urlBase = window.location.origin + ( base ? base : window.location.pathname ); | |
var newUrl = urlBase + '?' + kvp.join( '&' ) + window.location.hash; | |
if ( historyUpdate ) { | |
window.history.replaceState( null, null, newUrl ); | |
} | |
return newUrl; | |
} | |
/** | |
* Parse Query String | |
* | |
* @param {string} query | |
*/ | |
const parse_query_string = function( query ) { | |
var vars = query.split("&"); | |
var query_string = {}; | |
for ( var i = 0; i < vars.length; i++ ) { | |
var pair = vars[i].split("="); | |
var key = decodeURIComponent(pair[0]); | |
var value = decodeURIComponent(pair[1]); | |
// If first entry with this name | |
if (typeof query_string[key] === "undefined") { | |
query_string[key] = decodeURIComponent(value); | |
// If second entry with this name | |
} else if (typeof query_string[key] === "string") { | |
var arr = [query_string[key], decodeURIComponent(value)]; | |
query_string[key] = arr; | |
// If third or later entry with this name | |
} else { | |
query_string[key].push(decodeURIComponent(value)); | |
} | |
} | |
return query_string; | |
} | |
/** | |
* Get URL Parameter | |
* | |
* Usage: | |
* var searchText = getUrlParameter( 'search' ); | |
* | |
* @param {string} sParam parameter name, e.g., "search" | |
* @param {string} link (optional) if you want to get the parameter from | |
* a different URL then current page url. | |
* @return {multiple} returns the 'value' of parameter | |
*/ | |
var getUrlParameter = function( sParam, link ) { | |
var sPageURL = ( link ) ? link : decodeURIComponent(window.location.search.substring(1)), | |
sURLVariables = sPageURL.split('&'), | |
sParameterName, | |
i; | |
for ( i = 0; i < sURLVariables.length; i++ ) { | |
sParameterName = sURLVariables[i].split('='); | |
if (sParameterName[0] === sParam) { | |
return sParameterName[1] === undefined ? true : sParameterName[1]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment