Skip to content

Instantly share code, notes, and snippets.

@kilork
Created May 19, 2011 05:07
Show Gist options
  • Select an option

  • Save kilork/980229 to your computer and use it in GitHub Desktop.

Select an option

Save kilork/980229 to your computer and use it in GitHub Desktop.
toUriParameter
function toUriParameter(obj, post) {
var key, value,
parts = [],
encode = post ? function(s) {
return window.encodeURIComponent(s).replace(/%20/g,'+');
} : window.encodeURIComponent,
iterator = function(item) {
parts.push(encode(key) + '=' + encode(item));
};
for (key in obj) {
if (obj.hasOwnProperty(key)) {
value = obj[key];
if (value instanceof Array) {
value.forEach(iterator);
} else {
parts.push(encode(key) + '=' + encode(value));
}
}
}
return parts.join('&');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment