I'd like to get something like
?key=value?key=value
out of a js dictionary that is
{
key:value,
key:value
}
If using jQuery, you can use jQuery.param
:
var params = { width:1680, height:1050 };
var str = jQuery.param( params );
// str is now 'width=1680&height=1050'
Otherwise, here is a function that does it:
function serialize(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
}
alert(serialize({test: 12, foo: "bar"}));
There is a much simpler way to do this now:
API for URLSearchParams
is here
var url = new URL('https://example.com/');
url.search = new URLSearchParams({blah: 'lalala', rawr: 'arwrar'});
console.log(url.toString()); // https://example.com/?blah=lalala&rawr=arwrar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With