Last active
August 29, 2015 13:57
-
-
Save snorpey/9513872 to your computer and use it in GitHub Desktop.
converts an object to an url parameter string
This file contains hidden or 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
/*global define*/ | |
define( | |
function () | |
{ | |
// serializeURL({foo:'bar',one:'two'}) => '?foo=bar&one=two' | |
// serializeURL({foo:'bar',one:'two'}, 'prefix') => '?prefix[foo]=bar&prefix[one]=two' | |
function serializeURL( obj, prefix ) | |
{ | |
var str = [ ]; | |
for ( var p in obj ) | |
{ | |
var k = prefix ? prefix + '[' + p + ']' : p, v = obj[p]; | |
str.push( typeof v == 'object' ? serializeURL( v, k ) : encodeURIComponent( k ) + '=' + encodeURIComponent( v ) ); | |
} | |
return '?' + str.join( '&' ); | |
} | |
return serializeURL | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment