-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: URL.setSearchParams() #828
Comments
We could maybe add a |
I think a
I think there are a few options here.
Of these, number 2 would be my preference. |
I'm confused by this feature request, and in particular why the Given this code: const usp = new URLSearchParams("foo=bar");
const url = new URL("https://example.com/");
url.search = usp; you get the desired result: The section "What solutions exist today?" in the OP seems to be implying this isn't good enough. But I can't understand exactly when. Can you give a concrete example of a All the examples in "How would you solve it?" work today with the |
What problem are you trying to solve?
Effectively, for a URL object, setting the
searchParams
(and by extensionsearch
) to some URLSearchParams object.Currently, the
searchParams
property is read-only (see: URL: searchParams property). Hence, to update the searchParams, one would have to update thesearch
string property manually instead.This is not ideal as developers may want to work in terms of objects rather than strings (
new URLSearchParams({ foo: "bar", baz: "qux" }
) is much more readable compared to"?foo=bar&baz=qux"
).Being able to set the searchParams property without having to go through a string intermediate can be a pretty common case, such as appending multiple entries at once, or initializing with some default values.
What solutions exist today?
Today, to update the searchParams, one would either have to run the
delete()
method for all current entries andset()
for all new entries (which is impractical) or update thesearch
string property manually such as:However, since
URLSearchParams.toString()
andURL.search
are not the same value, this is slightly more complicatedThe
toString()
does not include the question mark?
, whileURL.search
does. This doesn't mean just by appending "?" that the proper value, since for both, an empty string is equivalent to a null query.How would you solve it?
I am not certain on the implementation details of the function, since just implementing the
setUrlSearchParams()
shown earlier method seems redundant (as you would have to parse the string into aURLSearchParams
object to get the new.searchParams
property), but in general, having a functionURL.setSearchParams
can make some tasks easier.For instance, for the cases I mentioned:
Intializing a URL with default values
Appending multiple entries
Anything else?
EDIT: I apologize, I just noticed this is very similar to #413.
The text was updated successfully, but these errors were encountered: