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.

Revisions

  1. kilork created this gist May 19, 2011.
    22 changes: 22 additions & 0 deletions toUriParameter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    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('&');
    }