Skip to content

Instantly share code, notes, and snippets.

@joeegan
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save joeegan/333162ee94abff673517 to your computer and use it in GitHub Desktop.

Select an option

Save joeegan/333162ee94abff673517 to your computer and use it in GitHub Desktop.
(function() {
var params = /(?:&|\?)(.+)/g.exec(window.location.href);
var paramsObj = {};
if (params && Object.keys(params).length) {
params[1].split('&').forEach(function(strPair) {
var splitPair = strPair.split('=');
paramsObj[splitPair[0]] = splitPair[1];
});
}
function applyParams(){
var url = window.location.href.match(/^[^&|?]+(?:[^&|?])/g);
var isFirst = true;
for (var key in paramsObj) {
var ampersand = isFirst ? '?' : '&';
var value = paramsObj[key];
if (value) {
url += ampersand + key + '=' + paramsObj[key];
} else {
url += ampersand + key;
}
isFirst = false;
}
return url;
}
function getOnOffState(currentValue){
return (currentValue == 'on') ? 'off' : 'on';
}
window.addParam = function(key, value){
var currentValue = paramsObj[key];
var value = value || getOnOffState(currentValue);
paramsObj[key] = value;
return applyParams();
}
window.getCookie = function(key){
var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
return keyValue ? keyValue[2] : null;
}
document.cookie = 'paramtoggler=' + escape(data);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment