Link on own page with set ID and base href - ToC

Viewed 50

I need to create via HTML a ToC - basically something like:

  1. First item
  2. Second item ...

I know that I can create e.g. First item as <h1 id="first">First item</h1> and than in the ToC something like

<li><a href="#first">First item</a></li>
<li><a href="#second">Second item</a></li>

The main problem I am facing is that the <base href="myService"> is set. Therefore the link to the own page won't work by default since the base-href is taken into account.

Is there a proper way to generate a ToC with set base href?

2 Answers

you can just make it more easy by adding the base url with the id and then when someone click on the link it will send him to the id like this:

<li><a href="127.0.0.1:8000#first">First item</a></li>

or any other urls

When you specify a <base> URL, then all relative links on that page go to that URL. That's what it is for, so avoid using it if you don't want that behavior.

If you must use <base> for some reason, you'll have to use absolute URLs for all your anchors by specifying the URL of the page. However, this can cause a problem if the URL of the page changes then the links need to be updated too. To solve this problem, you can use JavaScript to get the absolute location of the document and add the anchor at the end. Something like this:

<li><a href="javascript:;" onclick="location.hash='#first';">First item</a></li>
<li><a href="javascript:;" onclick="location.hash='#second';">Second item</a></li>
Related