This page contains the XML for <SCRIPT> DOMException object documentation.
Return to W3C DOM Parser Documentation Main Page
-
CONTENTS
-
DOMException - object description
-
-
DOMException - code property
-
-
DOMException.INDEX_SIZE_ERR (Error Code 1)
-
DOMException.DOMSTRING_SIZE_ERR (Error Code 2)
-
DOMException.HIERARCHY_REQUEST_ERR (Error Code 3)
-
DOMException.WRONG_DOCUMENT_ERR (Error Code 4)
-
DOMException.INVALID_CHARACTER_ERR (Error Code 5)
-
DOMException.NO_DATA_ALLOWED_ERR (Error Code 6)
-
DOMException.NO_MODIFICATION_ALLOWED_ERR (Error Code 7)
-
DOMException.NOT_FOUND_ERR (Error Code 8)
-
DOMException.NOT_SUPPORTED_ERR (Error Code 9)
-
DOMException.INUSE_ATTRIBUTE_ERR (Error Code 10)
-
DOMException.INVALID_STATE_ERR (Error Code 11)
-
DOMException.SYNTAX_ERR (Error Code 12)
-
DOMException.INVALID_MODIFICATION_ERR (Error Code 13)
-
DOMException.NAMESPACE_ERR (Error Code 14)
-
DOMException.INVALID_ACCESS_ERR (Error Code 15)
DOMException - object description
DOMExceptions are raised when an operation that is impossible to occur has been
detected.
NOTE: When catching errors, do not use the following construct:
try {
//some code here
}
catch (DOMException) {
//error checking code here
}
If the above code construct is used, there will be a naming conflict between the variable
DOMException and the DOMException class which will prevent the reading of the built-in
DOMException constants.
To properly catch a DOMException, use any variable name in your catch block other than
DOMException. For example:
try {
//some code here
}
catch (e) {
//error checking code here
}
will work properly.
DOMException - code property
W3C DOM Level 1
DOMException.code;
accepts:
N/A
returns:
int - a DOMException error code ID
The DOMException object has a single property that returns a error code ID. The following
error codes are possible:
DOM Level 1 Exceptions
|
Error Name
|
Error Code ID
|
DOMException.INDEX_SIZE_ERR
|
1
|
DOMException.DOMSTRING_SIZE_ERR
|
2
|
DOMException.HIERARCHY_REQUEST_ERR
|
3
|
DOMException.WRONG_DOCUMENT_ERR
|
4
|
DOMException.INVALID_CHARACTER_ERR
|
5
|
DOMException.NO_DATA_ALLOWED_ERR
|
6
|
DOMException.NO_MODIFICATION_ALLOWED_ERR
|
7
|
DOMException.NOT_FOUND_ERR
|
8
|
DOMException.NOT_SUPPORTED_ERR
|
9
|
DOMException.INUSE_ATTRIBUTE_ERR
|
10
|
DOM Level 2 Exceptions
|
Error Name
|
Error Code ID
|
DOMException.INVALID_STATE_ERR
|
11
|
DOMException.SYNTAX_ERR
|
12
|
DOMException.INVALID_MODIFICATION_ERR
|
13
|
DOMException.NAMESPACE_ERR
|
14
|
DOMException.INVALID_ACCESS_ERR
|
15
|
NOTE: Text descriptions of the above error code ID's can
be obtained by calling the
DOMImplementation.translateErrCode
method.
DOMException.INDEX_SIZE_ERR (Error Code 1)
W3C DOM Level 1
A DOMException.INDEX_SIZE_ERR exception is thrown whenever a
index or size is negative, or greater than the allowed value.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello World"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the document root
var docRoot = domDoc.getDocumentElement();
//get the TAG1 node
var tag1 = docRoot.getFirstChild();
//get the "Hello World" text
var textNode = tag1.getFirstChild();
//The following should raise a DOMException.INDEX_SIZE_ERR
try {
alert(textNode.substringData(15, 5))
}
catch (e) {
if (e.code == DOMException.INDEX_SIZE_ERR) {
//display the error message
alert(parser.translateErrCode(e.code));
}
else {
alert("unexpected exception");
alert(e.code);
}
}
}// end function xmljsDOMExample
DOMException.DOMSTRING_SIZE_ERR (Error Code 2)
W3C DOM Level 1
XML for <SCRIPT> does not throw an exception of this type in its implementation
of the W3C DOM Model.
DOMException.HIERARCHY_REQUEST_ERR (Error Code 3)
W3C DOM Level 1
A DOMException.HIERARCHY_REQUEST_ERR exception is thrown whenever an
insertion method cannot insert the node due to a violation of DOM rules.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello World"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the document root
var docRoot = domDoc.getDocumentElement();
//get the TAG1 node
var tag1 = docRoot.getFirstChild();
//The following should raise a DOMException.HIERARCHY_REQUEST_ERR
try {
tag1.insertBefore(tag1, docRoot );
}
catch (e) {
if (e.code == DOMException.HIERARCHY_REQUEST_ERR) {
//display the error message
alert(parser.translateErrCode(e.code));
}
else {
alert("unexpected exception");
alert(e.code);
}
}
}// end function xmljsDOMExample
DOMException.WRONG_DOCUMENT_ERR (Error Code 4)
W3C DOM Level 1
A DOMException.WRONG_DOCUMENT_ERR exception is thrown whenever a
DOM manipulation method tries to work with nodes that are part of two
different DOMDocuments. You must use the
DOMDocument.importNode
method in order to import a DOMNode from one document to another.
Example:
function xmljsDOMExample() {
var xml1;
xml1 = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "<DEEPNODE>"
+ "Hello 1"
+ "</DEEPNODE>"
+ "</TAG1>"
+ "</ROOT>";
var xml2;
xml2 = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT2>"
+ "<TAG2>"
+ "Hello 2"
+ "</TAG2>"
+ "</ROOT2>";
//instantiate the W3C DOM Parsers
var parser1 = new DOMImplementation();
var parser2 = new DOMImplementation();
//load the XML into the parsers and get the DOMDocuments
var domDoc1 = parser1.loadXML(xml1);
var domDoc2 = parser2.loadXML(xml2);
//get the root nodes
var docRoot1 = domDoc1.getDocumentElement();
var docRoot2 = domDoc2.getDocumentElement();
//get tag1 of the first XML
var tag1 = docRoot1.getElementsByTagName("TAG1").item(0);
//try to append tag1 to the second xml
//this should fail because the tag1 is
//not part of the second XML document
//The following should raise a DOMException.WRONG_DOCUMENT_ERR
try {
docRoot2.appendChild(tag1);
}
catch (e) {
if (e.code == DOMException.WRONG_DOCUMENT_ERR) {
//display the error message
alert(parser1.translateErrCode(e.code));
}
else {
alert("unexpected exception");
alert(e.code);
}
}
}// end function xmljsDOMExample
DOMException.INVALID_CHARACTER_ERR (Error Code 5)
W3C DOM Level 1
A DOMException.INVALID_CHARACTER_ERR is thrown whenever an
invalid or illegal character is specified, such as in a name.
NOTE: Names in XML can contain
English letters (of any case), numbers (0-9), underscores (_), periods(.)
and hyphens (-). Names cannot begin with a number, period or hyphen.
Names can also contain a colon, but the use of colons outside of namespaces
should be avoided.
XML for <SCRIPT> uses the following regular expression to determin
if a name is valid:
/^[a-zA-Z_:][a-zA-Z0-9\.\-_:]*$/
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//The following should raise a DOMException.INVALID_CHARACTER_ERR
//because element names cannot contain the "*" character
try {
domDoc.createElement("***");
}
catch (e) {
if (e.code == DOMException.INVALID_CHARACTER_ERR) {
//display the error message
alert(parser.translateErrCode(e.code));
}
else {
alert("unexpected exception");
alert(e.code);
}
}
}// end function xmljsDOMExample
DOMException.NO_DATA_ALLOWED_ERR (Error Code 6)
W3C DOM Level 1
XML for <SCRIPT> does not throw an exception of this type in its implementation
of the W3C DOM Model.
DOMException.NO_MODIFICATION_ALLOWED_ERR (Error Code 7)
W3C DOM Level 1
A DOMException.NO_MODIFICATION_ALLOWED_ERR exception is thrown whenever a
DOM manipulation method is called on a read-only DOM element.
NOTE: XML for <SCRIPT> always defaults nodes to
read/write. To make a node read-only, JavaScript developers can set a DOMNodes's
internal _readonly property to true.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello World"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the document root
var docRoot = domDoc.getDocumentElement();
//get the TAG1 node
var tag1 = docRoot.getFirstChild();
//get the "Hello" text node
var textNode = tag1.getFirstChild();
//set the textNode's internal read-only property
textNode._readonly = true;
//The following should raise a DOMException.NO_MODIFICATION_ALLOWED_ERR
try {
textNode.setNodeValue("foo");
}
catch (e) {
if (e.code == DOMException.NO_MODIFICATION_ALLOWED_ERR) {
//display the error message
alert(parser.translateErrCode(e.code));
}
else {
alert("unexpected exception");
alert(e.code);
}
}
}// end function xmljsDOMExample
DOMException.NOT_FOUND_ERR (Error Code 8)
W3C DOM Level 1
A DOMException.NOT_FOUND_ERR exception is thrown whenever a
node is referenced in a context where it does not exist (such as by name).
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE>"
+ "<TAG1 foo=\"goo\" id=\"tag1_id\">"
+ "Hello World"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//find tag1 by searching by its id
var tag1 = domDoc.getElementById("tag1_id");
//get a DOMNamedNodeMap of the attributes for tag1
var namedNodeMap = tag1.getAttributes();
//The following should raise a DOMException.NOT_FOUND_ERR
//because there is no DOMAttr with the name of "typo"
try {
namedNodeMap.removeNamedItem("typo");
}
catch (e) {
if (e.code == DOMException.NOT_FOUND_ERR) {
//display the error message
alert(parser.translateErrCode(e.code));
}
else {
alert("unexpected exception");
alert(e.code);
}
}
}// end function xmljsDOMExample
DOMException.NOT_SUPPORTED_ERR (Error Code 9)
W3C DOM Level 1
XML for <SCRIPT> does not throw an exception of this type in its implementation
of the W3C DOM Model.
DOMException.INUSE_ATTRIBUTE_ERR (Error Code 10)
W3C DOM Level 1
A DOMException.INUSE_ATTRIBUTE_ERR exception is thrown whenever
an attempt is made to add an attribute to a DOM that is already in use elsewhere.
In order to re-use attributes in other elements, JavaScript developers must explicitly
clone the DOMAttr nodes before inserting them.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE>"
+ "<TAG1 foo=\"goo\" id=\"tag1_id\">"
+ "Hello World"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//find tag1 by searching by its id
var tag1 = domDoc.getElementById("tag1_id");
//get a DOMNamedNodeMap of the attributes for tag1
var namedNodeMap = tag1.getAttributes();
//get the DOMAttr node with the name of "foo"
var fooNode = namedNodeMap.getNamedItem("foo");
//The following should raise a DOMException.INUSE_ATTRIBUTE_ERR
//because fooNode cannot be added to the docRoot's
//DOMNamedNodeMap without first being cloned
try {
docRoot.getAttributes().setNamedItem(fooNode);
}
catch (e) {
if (e.code == DOMException.INUSE_ATTRIBUTE_ERR) {
//display the error message
alert(parser.translateErrCode(e.code));
}
else {
alert("unexpected exception");
alert(e.code);
}
}
}// end function xmljsDOMExample
DOMException.INVALID_STATE_ERR (Error Code 11)
W3C DOM Level 2
XML for <SCRIPT> does not throw an exception of this type in its implementation
of the W3C DOM Model.
DOMException.SYNTAX_ERR (Error Code 12)
W3C DOM Level 2
A DOMException.SYNTAX_ERR exception is thrown whenever
the XML string cannot be parsed due to an error in syntax.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE>"
+ "<TAG1 foo=\"goo\" id=\"tag1_id\">"
+ "Hello World"
+ "</TAG1>"
+ "<ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//The following should raise a DOMException.SYNTAX_ERR
//because ROOTNODE is not closed properly
try {
var domDoc = parser.loadXML(xml);
}
catch (e) {
if (e.code == DOMException.SYNTAX_ERR) {
//display the error message
alert(parser.translateErrCode(e.code));
}
else {
alert("unexpected exception");
alert(e.code);
}
}
}// end function xmljsDOMExample
DOMException.INVALID_MODIFICATION_ERR (Error Code 13)
W3C DOM Level 2
XML for <SCRIPT> does not throw an exception of this type in its implementation
of the W3C DOM Model.
DOMException.NAMESPACE_ERR (Error Code 14)
W3C DOM Level 2
A DOMException.NAMESPACE_ERR exception is thrown whenever
an attempt is made to create or change an object in a way which
is incorrect with regard to namespaces.
The most common causes of a DOMException.NAMESPACE_ERR are the following:
-
A null namespace prefix
-
A namespace prefix of "xml" that is not in the namespaceURI of "http://www.w3.org/XML/1998/namespace"
-
A namespace prefix of "xmlns" that is not in the namespaceURI of "http://www.w3.org/2000/xmlns/"
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ns1:ROOTNODE xmlns:ns1=\"http://xmljs.sf.net/ns1\">"
+ "<ns1:TAG1>"
+ "Hello World"
+ "</ns1:TAG1>"
+ "</ns1:ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//The following should raise a DOMException.NAMESPACE_ERR
//because the prefix is null (nothing before the ":")
try {
var nsFoo = "http://xmljs.sf.net/foo";
var newNode = domDoc.createElementNS(nsFoo, ":ElementName");
}
catch (e) {
if (e.code == DOMException.NAMESPACE_ERR) {
var domImp = domDoc.getImplementation();
//display the error message
alert(domImp.translateErrCode(e.code));
}
else {
alert("unexpected exception");
alert(e.code);
}
}
}// end function xmljsDOMExample
DOMException.INVALID_ACCESS_ERR (Error Code 15)
W3C DOM Level 2
XML for <SCRIPT> does not throw an exception of this type in its implementation
of the W3C DOM Model.