HTML DOM
HTML DOM baseURI Property
The baseURI property returns the absolute base URL of the document. The base URL is the reference against which all relative URLs on the page are resolved.
Definition and Usage
document.baseURI returns the absolute base URL used to resolve relative URLs in the document. By default this is the URL of the page itself, but it can be overridden by a <base> element placed in the <head>.
The property is read-only. It is useful when you need to build absolute links or debug why relative paths are resolving to unexpected addresses.
Syntax
document.baseURIExample
Click the button to print the current document's base URI into the output box.
<!DOCTYPE html>
<html>
<body>
<button onclick="showBase()">Show baseURI</button>
<p id="out"></p>
<script>
function showBase() {
document.getElementById("out").textContent =
"baseURI = " + document.baseURI;
}
</script>
</body>
</html>More Examples
A <base> element changes the base URL for the whole page. Compare the baseURI reported below with the actual page address - it reflects the <base> href.
<!DOCTYPE html>
<html>
<head>
<base href="https://example.com/docs/">
</head>
<body>
<p id="out"></p>
<script>
document.getElementById("out").textContent =
"baseURI is now: " + document.baseURI;
</script>
</body>
</html>If a page has no <base> element, document.baseURI is simply the same as the page's own URL (document.URL).
Key Takeaways
- baseURI returns the absolute base URL of the document.
- It is the reference used to resolve all relative URLs.
- A <base> element in the <head> overrides the default value.
- The property is read-only.
