10

My understanding according to this post is that the default base href is '/'. This scotch tutorial says to include this base href:

<!-- ./index.html  -->
<base href="/">

For a basic app, is this strictly necessary, or will browsers assume that '/' is the base href for relative paths anyway?

Community
  • 1
  • 1
Elliott Beach
  • 10,459
  • 9
  • 28
  • 41

2 Answers2

11

will browsers assume that '/' is the base href for relative paths anyway?

No, they will not.

The HTML spec specifies how relative URLs are resolved. It's not too hard to work through the specification and definitions to find that when a relative URL is resolved against a document that does not have a base URL specified via a <base> element, the effective ("fallback") base URL is not guaranteed to be /. There are a few alternatives, but in the usual case, the document's fallback base URL is taken as the document's own URL. Typically, that's not /.

It's not so clear whether it's a good idea to specify / as every document's base URL. It does mean that every relative URL will resolve the same way, regardless of the document in which it appears; that could be taken as a plus. On the other hand, it differs from the default behavior for resolving relative URLs, which could be a source of bugs. Certainly, if you choose to do it then you should do it consistently throughout the application.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
1

The base is the location the page is in. Every relative URL is relative to that base. Let us suppose you are at

http://a/b/c

If you have a link inside this page with this code:

<a href="../d">abc</a>

Then by clicking on the link, the target will be

http://a/d

So, your base is at

http://a/b

because c is inside that. Now that you understand what base is, take a look here. You will see that besides href, you can set target as well, specifying where links should open.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175