YouTip LogoYouTip

Prop Element Baseuri

# XML DOM: baseURI Property The `baseURI` property is a standard property of the XML DOM Element object. It allows developers to retrieve the absolute base URL (URI) of a document or a specific node, which is highly useful for resolving relative URLs within XML and HTML documents. --- ## Definition and Usage The `baseURI` property returns the absolute base URI of the document or element node. ### How the Base URI is Determined: 1. **Document Location**: By default, the base URI is the URL/location of the document itself (e.g., `https://example.com/data.xml`). 2. **HTML `` Tag**: In HTML documents, if a `` tag is present, its `href` attribute value will define the base URI for all elements in the document. 3. **`xml:base` Attribute**: In XML documents, if an element or any of its ancestors has an `xml:base` attribute, that value is used to calculate the base URI for that element. --- ## Syntax ```javascript elementNode.baseURI ``` ### Return Value * **Type**: `String` * **Description**: Returns a string representing the absolute base URI of the node. It returns `null` if the document does not have a base URI (for example, if it was created entirely in-memory and has not been saved or loaded from a specific location). --- ## Code Example The following example demonstrates how to load an XML document (`books.xml`) using a helper function `loadXMLDoc()` and retrieve the base URI of its first `` element. ### Sample XML File (`books.xml`) ```xml Everyday Italian Giada De Laurentiis 2005 30.00 ``` ### JavaScript Implementation ```javascript // Load the XML document var xmlDoc = loadXMLDoc("books.xml"); // Select the first element var firstBook = xmlDoc.getElementsByTagName("book"); // Retrieve and display the base URI of the element console.log("Document location: " + firstBook.baseURI); ``` ### Expected Output If the XML file is hosted at `https://www.example.com/assets/books.xml`, the output will be: ```text Document location: https://www.example.com/assets/books.xml ``` --- ## Practical Considerations ### 1. Read-Only Property The `baseURI` property is **read-only**. You cannot modify the base URI of a node directly by assigning a value to this property. ### 2. Browser Compatibility The `baseURI` property is widely supported across all modern web browsers and standard XML DOM parsers: * Google Chrome * Mozilla Firefox * Apple Safari * Microsoft Edge * Opera ### 3. Difference Between `baseURI` and `documentURI` * **`documentURI`**: This property belongs to the `Document` object and returns the URI of the document itself. * **`baseURI`**: This property belongs to the `Node` interface (and is thus available on elements, attributes, and text nodes). It can change dynamically depending on the presence of `` tags (HTML) or `xml:base` attributes (XML).
← Prop Element ChildnodesProp Element Attributes β†’