Last active
December 15, 2015 05:19
-
-
Save ingowennemaring/5208351 to your computer and use it in GitHub Desktop.
Handle URL parameters: get, update, buildURL
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 getURLParams() { | |
var i = 0, | |
params = {}, | |
tmpParam = [], | |
pairs = window.location.href | |
.slice(window.location.href.indexOf('?') + 1) | |
.replace(window.location.hash, '') | |
.split('&'); | |
for(; i < pairs.length; i++) { | |
tmpParam = pairs[i].split('='); | |
params[tmpParam[0]] = tmpParam[1]; | |
} | |
return params; | |
} | |
function updateURLParams( key, val ) { | |
var params = getURLParams(); | |
params[key] = val; | |
return params; | |
} | |
function buildURLwithParams( params ) { | |
var i = 0, | |
url = '', | |
paramsString = '', | |
loc = document.location, | |
protocol = loc.protocol, | |
host = loc.host, | |
pathname = loc.pathname, | |
hash = loc.hash; | |
for(key in params) { | |
if (params.hasOwnProperty(key)) { | |
if(i > 0) { | |
paramsString += '&'; | |
} | |
paramsString += key + '=' + params[key]; | |
i++; | |
} | |
} | |
if( paramsString !== '' ) { | |
paramsString = '?' + paramsString; | |
} | |
url = protocol + '//' + host + pathname + paramsString + hash; | |
return url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment