Re: [whatwg/url] Support relative URLs (#531)

> Thanks for your reply Seth, could you perhaps go into some more detail as to why you want to avoid window.location and where these relative URLs are common?

> @annevk points out that for such a change to be considered, they need examples where relative references are useful in a browser context, so we're looking for such use cases.

I think that there are natural cases where generating relative URLs is useful in a web app.

Suppose that some component A generates a link to another component B which takes a query parameter. For example, component A is at `http://example.com/inbox` and component B is at `http://example.com/message?id=<the ID of a message>`.

One approach is to generate an absolute URL, so that the DOM will be like `<a href="http://example.com/message?id=abcde">Open message</a>`. But this introduces unnecessary dependency on the domain name. This causes inconveniences such as that the domain name has to be faked in unit tests.

Another approach is to generate a relative URL, so that the DOM will be like `<a href="/message?id=abcde">Open message</a>`, and leave the relative-to-absolute conversion to the browser. To do so, it would be useful to write code like
```
const url = new URL('/message');
url.searchParams.set('id', messageId);
const link = createElement('a');
link.href = url.href;
...
```
but this code does not currently work because `new URL('/message')` throws.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/url/issues/531#issuecomment-831972827

Received on Tuesday, 4 May 2021 14:11:09 UTC