I need to generate the following
deviceSearch?q=4100&origin=pdp
instead I am seeing
deviceSearch?q=4100%26origin%3Dpdp
Googling this problem it seems I cannot do this.
I tried the following that did not work
url.searchParams.set('q', item.selectionText);
url.searchParams.append(name: origin, value: pdp);
I tried joining the text which did not work.
url.searchParams.set('q', item.selectionText.concat('&origin=pdp'));
I also tried using escape code ” before those characters. I assume I am missing something in addition to any real knowledge of javascript.
2
Answers
You can just call
set
again, same way you are setting theq
parameter.&
symbol is added by theURLSeachParams
class itself.You can also use
.append
, the syntax would be the same:But that might not be what you’re trying to do. Difference is that
set
will override the value if you try set the same parameter twice, andappend
will let add the same parameter multiple times.Each of these are separate query parameters of the URL string. You don’t manually add the
&
or=
symbols as they are a result of the parameter definitions. Theurl.searchParams.set
takes the parameter name as the first argument and the value assigned as the second argument. That creates the key value pair or (parameter=value) The&
is a result of more than one parameter being set with that method.If you called this method 4 different times you would have this example URL string.
// /deviceSearch?q=4100&origin=php&name=test&age=12