Met Node Lookupnamespaceuri
## XML DOM lookupNamespaceURI() Method
The `lookupNamespaceURI()` method is a standard DOM Node interface method. It returns the namespace URI associated with a specified prefix on a given node, searching up the XML/HTML tree if necessary.
---
## Definition and Usage
The `lookupNamespaceURI()` method looks up the namespace URI associated with a given prefix, starting from the current node.
If the prefix is found within the scope of the node (either declared on the node itself or on its ancestor elements), the method returns the corresponding namespace URI string. If the prefix cannot be resolved, it returns `null`.
---
## Syntax
```javascript
nodeObject.lookupNamespaceURI(prefix)
```
### Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `prefix` | String | **Required.** The prefix whose namespace URI you want to find. If this parameter is `null` or an empty string, the method returns the default namespace URI. |
### Return Value
* **String**: The namespace URI associated with the specified prefix.
* **null**: Returned if no matching namespace URI is found, or if the prefix is not declared.
---
## Code Example
The following example demonstrates how to use the `lookupNamespaceURI()` method. It loads an XML document containing namespaces (`books_ns.xml`) and looks up the namespace URI associated with the prefix `"c"` starting from the first `` element.
### Sample XML File (`books_ns.xml`)
```xml
Learning XML
John Doe
```
### JavaScript Implementation
```javascript
// Load the XML document
var xmlDoc = loadXMLDoc("books_ns.xml");
// Select the first element
var x = xmlDoc.getElementsByTagName("book");
// Look up the namespace URI associated with the prefix "c"
var namespaceURI = x.lookupNamespaceURI("c");
// Output the result
document.write(namespaceURI);
```
### Output
```text
http://www.youtip.co/w3cnote/
```
---
## Considerations & Best Practices
1. **Scope Resolution**: The lookup is hierarchical. If the prefix is not declared on the current node, the browser will traverse up the parent chain (ancestor elements) until it finds the declaration or reaches the root node.
2. **Default Namespace**: If you pass `null` or an empty string `""` as the `prefix` parameter, the method will return the default namespace URI of the node's scope (i.e., the namespace declared via `xmlns="..."`).
3. **Browser Compatibility**: This method is part of the DOM Level 3 Core specification and is widely supported in all modern web browsers (Chrome, Firefox, Safari, Edge, and Opera).
YouTip