I would like to form a link on the client end.
I think that need to be urlencoded but I am pretty confused with this. Is this right?
var link = document.createElement("a");
//addParams function just replaces the 0th and 1th occurrence with values
a.link = url.addParams(valueO, value1); // url is /home/testpage.aspx?{0}={1}
Shouldn’t that be
link.href = url.addParams(valueO, value1);?And both
value0andvalue1should be sent (individually) through eitherescape()orencodeURIComponent()(I believe the latter is recommended overescape()). You probably will want to do this inaddParams()before you replace{0}and{1}in your URL string.You could also do something like
addParams(encodeURIComponent(value0), encodeURIComponent(value1))but in my opinion, that’s asking for trouble if you forget to encode some values in a call somewhere. It’d be better to move the encoding into the function itself.Here’s an explanation of what
escape(), encodeURI(), encodeURIComponent()do and how they’re different and when to use each.