When I use the HTML <base>
tag to define a base URL for all relative links on a page, anchor links also refer directly to the base URL. Is there a way to set the base URL that would still allow anchor links to refer to the currently open page?
For example, if I have a page at http://example.com/foo/
:
Current behaviour:
<base href="http://example.com/" />
<a href="bar/">bar</a> <!-- Links to "http://example.com/bar/" -->
<a href="#baz">baz</a> <!-- Links to "http://example.com/#baz" -->
Desired behaviour:
<base href="http://example.com/" />
<a href="bar/">bar</a> <!-- Links to "http://example.com/bar/" -->
<a href="#baz">baz</a> <!-- Links to "http://example.com/foo/#baz" -->
<a href="foo#baz">
. – Pentecostalhttp://server/
and you tell it to navigate to#baz
, it will gohttp://server/#baz
, that's how relative URLS work, and that's what using<base>
does, it changes what relative URLs are based on. If that's not what you want, your link should not be relative, or it should be relative to the base's href (foo#baz
) – Steakhousehref="#xyz"
always refers to an anchor on the the current page, not a completely different page. After all the entire point of "#" is to jump to a section of a document, NOT to load another page. The fact it's possible with<base href
to make "#" by itself (without any path component) load a completely different page when it doesn't permit that behavior in any other circumstance I would argue not only a technical deficiency, but even a security flaw. – Oast