What is the use of document.baseURI property?
Overview
The document.baseURI property in DOM returns the HTML document's base document.URL.
Note: To read more on
document.URLproperty, 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 theonclickevent. It triggers thefetchbaseURI()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
baseURIvariable. - Lines 15–16: We first use
document.getElementById()method to fetch the<p>tag. Then, we useinnerHTMLproperty to populate it with the value ofbaseURI.
Note: To read more on the
innerHTMLproperty anddocument.getElementById()method, please refer to the following links:
Free Resources