url
url copied to clipboard
Invoking serializer "exclude fragment" from API
This is a bit of a long shot, but I figured I'd ask...
I found myself needing to serialize a URL via the API, but with "exclude fragment" being true.
I'm wondering, could we overload the serializer to accept {excludeFragment: true}... imagine something like:
const str = url.toString({ excludeFragment: true });
const json = url.toJSON({ excludeFragment: true });
It's a bit unconventional, but taking the optional argument would be nice for completeness.
This technically isn't supported by Web IDL at the moment. Could you instead do
function toStringWithoutFragment(url) {
const u2 = new URL(url);
u2.hash = '';
return String(u2);
}
Yeah, that was literally what I was doing (and why I ended up here). Having to create a new URL is not amazing, but it's not the end of the world either.