All pastes #1018774 Raw Edit

Untitled

public text v1 · immutable
#1018774 ·published 2008-05-15 07:02 UTC
rendered paste body
// Serialize an array of form elements or a set of
// key/values into a query string
param: function( a , pn, d) {
	if (!d)
		d=0;

	var t = typeof(a);
	// We're at the end of the line so return it.
	if (t == 'string' || t == 'number' || t == 'boolean' || t == 'undefined')
		return pn+'='+encodeURIComponent(a);

	var s = [];
	if ( (a.constructor == Array && this.name) || a.jquery )
		// Serialize the form elements
		jQuery.each( a, function(){
			s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
		});
	// Otherwise, assume that it's an object of key/value pairs
	else
		for ( var j in a ) {
			d++;
			// prevent infinite recursion.  max of 10 levels deep.
			if (d>10)
				break;
			if (!pn)
				// recurse into siblings. with this as parent.
				s.push(jQuery.param(a[j], encodeURIComponent(j)),d);
				else
				// recurse into siblings.
				s.push(jQuery.param(a[j], pn+"["+encodeURIComponent(j)+"]"),d);

		}
	// Return the resulting serialization
	return s.join("&").replace(/%20/g, "+");
}