You are here: Reference > JavaScript > client-side > HTML DOM > objects > namespaces

namespaces collection

Browser support:
Represents a collection of namespaces that have been declared for custom elements in HTML.
Namespaces are useful to create custom elements, with custom functionality in HTML. To create a custom element, declare a namespace with the xmlns attribute or with the add method of the namespaces collection, add behaviors to it with the doImport method and use the local name of your namespace as a prefix for custom elements.
These kind of namespaces are different from namespaces in XML documents. If you need information about namespace handling in XML documents, please see the pages for the createAttributeNS and createElementNS methods.

Syntax:

Properties that reference the object:
object.namespaces
Related objects:

Possible members:

Properties:
length
Returns an integer that specifies the number of namespace objects in the current collection.

This property is read-only.
Methods:
[nameOrIndex]
Returns an object from the current collection by name or index.

Parameters:

nameOrIndex
Required. A string that specifies the name or a zero-based integer that specifies the position of the namespace to retrieve.

Return value:

Returns a namespace object.
add (name, URN [, URL])
Adds a new namespace to the current collection.

Parameters:

name
Required. String that specifies the name of the namespace.
URN
Required. String that specifies the Uniform Resource Name (URN) of the namespace.
URL
Optional. String that specifies the location of the behavior file that should be imported into the new namespace. It has the same effect as the doImport method.

Return value:

Returns the newly created namespace object
item (nameOrIndex)
Returns an object from the current collection by name or index.

Parameters:

nameOrIndex
Required. A string that specifies the name or a zero-based integer that specifies the position of the namespace to retrieve.

Return value:

Returns a namespace object.

Example HTML code 1:

This example illustrates the use of the namespaces collection:
Code
hover.htc
<html xmlns:myNS="http://help.dottoro.com/NS">
<head>
    <script type="text/javascript">
        var namespace = document.namespaces("myNS");
        namespace.doImport("hover.htc");

        function GetNSInfo () {
            var namespace = document.namespaces["myNS"];
            alert ("The name of the namespace is " + namespace.name);
            alert ("The URN of the namespace is " + namespace.urn);
        }
    </script>
</head>
<body>
    <myNS:span>Move the mouse over this text (element with behavior)</myNS:span><br />
    <span>Move the mouse over this text (element without behavior)</span>
    <br /><br />
    <button onclick="GetNSInfo ()">Get information about a namespace</button>
</body>
</html>
Did you find this example helpful? yes no

Example HTML code 2:

This example is equivalent to the previous one, but it uses the add method to declare a namespace:
Code
hover.htc
<html>
<head>
    <script type="text/javascript">
        var namespace = document.namespaces.add ("myNS", "http://help.dottoro.com/NS", "hover.htc");

        function GetNSInfo () {
            var namespace = document.namespaces["myNS"];
            alert ("The name of the namespace is " + namespace.name);
            alert ("The URN of the namespace is " + namespace.urn);
        }
    </script>
</head>
<body>
    <myNS:span>Move the mouse over this text (element with behavior)</myNS:span><br />
    <span>Move the mouse over this text (element without behavior)</span>
    <br /><br />
    <button onclick="GetNSInfo ()">Get information about a namespace</button>
</body>
</html>
Did you find this example helpful? yes no

External links:

User Contributed Comments

Post Content

Post Content