0

Is there any way to define a base link for a web page, meaning that all relative hrefs in the page start at that base location (different from the location of the web page)?

This is what I want:

<base = "../Main/Sub/Subsub1/">

The page I'm using this in is in a subfolder parallel to Main, but the content it will link to is under Main. I tried the above code, but it doesn't work

thf
  • 594
  • 1
  • 10
  • 22

1 Answers1

0

You can use js to generate the base url as below

<script>
var base = document.createChild('base');
// use logic to do ../
base.href = window.location.href.split('/').slice(0,-1).join('/')+'/Main/Sub/Subsub1/';
document.head.appendChild(base);
</script>

Or provide the absolute url to base href

The HTML element specifies the base URL to use for all relative URLs in a document. There can be only one element in a document.

Refer the MDN Docs

Dhananjai Pai
  • 5,914
  • 1
  • 10
  • 25