What is the use of document.baseURI property?

Overview

The document.baseURI property in DOM returns the HTML document's base Uniform Resource IdentifierURI refers to the string of characters which identifies a particular resource on the internet with the help of its name or location. (URI). The working of this property is quite similar to that of document.URL.

Note: To read more on document.URL property, please refer to this Answer.

Syntax

Now that we understand the primary use of the property, let us take a look at the syntax in the following code widget:


document.baseURI

Return value

The return value of this property is a string that represents the base URI of the document and it is only for read-only purposes.

Example

Let us take a look at a code example that makes use of this property:

Explanation

  • Lines 5–11: In this piece of code, we have the parent <div> tag, which encloses the content of the HTML page as seen in the output. The content of the HTML page can be further broken down into the following elements:
    • Line 6: This line has a <h3> tag that displays the heading at the top of our HTML page.
    • Line 8: Here, we have a <p> tag that we populate with the value of the base URI.
    • Line 10: We have a <button> for the user, which is listening for the onclick event. It triggers the fetchbaseURI() method.
  • Lines 12–18: We use the fetchbaseURI() method to retrieve the base URI of the document. The method's body composes of the following lines of code:
    • Line 14: Here, we assign the base URI of the document to baseURI variable.
    • Lines 15–16: We first use document.getElementById() method to fetch the <p> tag. Then, we use innerHTML property to populate it with the value of baseURI.

Note: To read more on the innerHTML property and document.getElementById() method, please refer to the following links:

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved