Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need base href='/'?

Tags:

html

angularjs

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?

like image 640
Elliott Beach Avatar asked Dec 19 '16 19:12

Elliott Beach


People also ask

Is BASE HREF required?

No, they will not. The HTML spec specifies how relative URLs are resolved.

What is the purpose of base href?

Definition and Usage The href attribute specifies the base URL for all relative URLs on a page.

Where should a base href be placed?

The <base> tag must have either an href or a target attribute present, or both. There can only be one single <base> element in a document, and it must be inside the <head> element.

What is -- BASE HREF in ng build?

--base-href If you deploy your Angular app to a subfolder, the '--base-href' is important to generate the correct routes. This parameter will update the <base href> tag inside the index. html. For example, if the index.


1 Answers

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.

like image 188
John Bollinger Avatar answered Oct 13 '22 01:10

John Bollinger