Skip to content

Instantly share code, notes, and snippets.

@Uysim
Last active November 23, 2016 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Uysim/d9b63876812b8b29a3cb8f3413519237 to your computer and use it in GitHub Desktop.
Save Uysim/d9b63876812b8b29a3cb8f3413519237 to your computer and use it in GitHub Desktop.
this script use for custom url params and redirect it
// new ParamsHandler({ location_id: object.id, page: 1 }).redirect();
window.ParamsHandler = (function() {
function ParamsHandler(params) {
this.params = params;
this.kvp = document.location.search.substr(1).split('&');
}
ParamsHandler.prototype.redirect = function() {
this._mapParams();
return document.location.search = this.kvp.join('&');
};
ParamsHandler.prototype._mapParams = function() {
var key, ref, results, value;
ref = this.params;
results = [];
for (key in ref) {
value = ref[key];
results.push(this._insertParam(key, value));
}
return results;
};
ParamsHandler.prototype._insertParam = function(key, value) {
var i, x;
console.log(key);
key = encodeURI(key);
value = encodeURI(value);
i = this.kvp.length;
x = void 0;
while (i--) {
x = this.kvp[i].split('=');
if (x[0] === key) {
x[1] = value;
this.kvp[i] = x.join('=');
break;
}
}
if (i < 0) {
return this.kvp.push(key + "=" + value);
}
};
return ParamsHandler;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment