Created
September 29, 2009 20:01
-
-
Save jfsiii/197252 to your computer and use it in GitHub Desktop.
obj2URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function obj2URL(obj, url) | |
{ | |
url = url || ''; | |
var parts = [], | |
push = function(key, val){ | |
parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(val)); | |
}, | |
qs; | |
for (var key in obj) { | |
if (obj.hasOwnProperty(key)) { | |
var val = obj[key]; | |
if (val instanceof Array) { | |
for (var i=0, l=val.length; i<l; i++) { push(key, val[i]); } | |
} else { | |
push(key, val); | |
} | |
} | |
} | |
qs = (/\?/.test(url) ? '&' : '?') + parts.join('&'); | |
return url + qs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment