[whatwg] [URL] Resolving against the base of the current page

Anne van Kesteren annevk at annevk.nl
Thu Jul 11 07:59:38 PDT 2013

On Thu, Jul 11, 2013 at 7:56 AM, Jake Archibald <jaffathecake at gmail.com> wrote:
> http://url.spec.whatwg.org/
> How would I create a URL relative to the page, but taking into account
> <base> (and anything else that may affect relative urls on the page)?


> It feels like the 2nd constructor parameter should default to the page's
> base url, and you could pass window.location.href in if you wanted to
> override <base>.
> So:
> <img src="cat.gif">
> new URL('cat.gif').href == document.querySelector('img').src;
> And:
> if (new URL('cat.gif').href !== new URL('cat.gif', location.href).href) {
>   // something is modifying the base url
> }

I think I'd prefer consistent results for the constructor across
scripts if you omit a base URL.


More information about the whatwg mailing list