Skip to content

Instantly share code, notes, and snippets.

@gilbox
Last active December 28, 2015 03:18
Show Gist options
  • Save gilbox/7433721 to your computer and use it in GitHub Desktop.
Save gilbox/7433721 to your computer and use it in GitHub Desktop.
Encodes a JavaScript object (with key/values) into a URI (ie: key1=value1&key2=value2...)
function encodeURIObject(o) {
var a = [];
for (k in o) {
a.push(encodeURIComponent(k) + '=' + encodeURIComponent(o[k]))
}
return a.join('&');
}
// test:
var uriStringTest = encodeURIObject({
key1: 'value1',
key2: 'value2'
});
console.log('result: ', uriStringTest);
// result: key1=value1&key2=value2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment