I would like to know where I can find the implementation code for jquery.param.
Rob
45.9k24 gold badges126 silver badges155 bronze badges
asked Jul 20, 2010 at 4:20
3 Answers 3
answered Jul 20, 2010 at 4:24
Comments
Here is the source directly from jQuery (MIT license):
jQuery.param = function( a, traditional ) {
var prefix,
s = [],
add = function( key, value ) {
// If value is a function, invoke it and return its value
value = jQuery.isFunction( value ) ? value() : ( value == null ? "" : value );
s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value );
};
// Set traditional to true for jQuery <= 1.3.2 behavior.
if ( traditional === undefined ) {
traditional = jQuery.ajaxSettings && jQuery.ajaxSettings.traditional;
}
// If an array was passed in, assume that it is an array of form elements.
if ( jQuery.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
// Serialize the form elements
jQuery.each( a, function() {
add( this.name, this.value );
});
} else {
// If traditional, encode the "old" way (the way 1.3.2 or older
// did it), otherwise encode params recursively.
for ( prefix in a ) {
buildParams( prefix, a[ prefix ], traditional, add );
}
}
// Return the resulting serialization
return s.join( "&" ).replace( r20, "+" );
};
Daniel Imms
50.5k19 gold badges157 silver badges170 bronze badges
answered Apr 29, 2015 at 13:28
3 Comments
Nate Barbettini
Welcome to Stack Overflow! Code-only answers are discouraged. Can you edit your post to describe where you got this code, perhaps?
Chubas
This is probably a correct answer, however, the lack of a source means that 1) no context is provided and 2) is very susceptible to be outdated. A link and a little explanation would improve this answer a lot
Daniel Imms
Please explain where you got the source from in the future, simply pasting the code here and claiming it as your own is against the jQuery license. I added proper attribution.
In case anyone else stumbles across this, it's in src/serialize.js now https://github.com/jquery/jquery/blob/master/src/serialize.js
Comments
lang-js