Definitely not _the_ most beautiful code I've written, but I still enjoy this little function for some reason. It's just pretty to me.
function addparams(query) {
return query.replace(/(&?)([^&=]+)=([^&]*)/g, function(m, amper, key, id) {
var val = document.getElementById(id).value;
return val.length > 0 ? amper + key + "=" + escape(val) : '';
});
}
All it does is take a query string template of sorts, grab the values to plug in, and then return an actual query string. For instance, if you had this on your page somewhere:
Nothing remotely complicated or amazing like the regex that finds prime numbers or the 12 line quicksort, but I just think the function is aesthetically pleasing.
Definitely not _the_ most beautiful code I've written, but I still enjoy this little function for some reason. It's just pretty to me.
All it does is take a query string template of sorts, grab the values to plug in, and then return an actual query string. For instance, if you had this on your page somewhere: Then we used it when doing some kind of ajax update. Assuming jQuery, (although that wasn't the case) something like this: Which would make an xhr GET request to: Nothing remotely complicated or amazing like the regex that finds prime numbers or the 12 line quicksort, but I just think the function is aesthetically pleasing.