What exactly can cause an "HIERARCHY_REQUEST_ERR: DOM Exception 3"-Error?

JavascriptJqueryDomexception

Javascript Problem Overview


How exactly does it relate to jQuery? I know the library uses native javascript functions internally, but what exactly is it trying to do whenever such a problem appears?

Javascript Solutions


Solution 1 - Javascript

It means you've tried to insert a DOM node into a place in the DOM tree where it cannot go. The most common place I see this is on Safari which doesn't allow the following:

document.appendChild(document.createElement('div'));

Generally, this is just a mistake where this was actually intended:

document.body.appendChild(document.createElement('div'));

Other causes seen in the wild (summarized from comments):

  • You are attempting to append a node to itself
  • You are attempting to append null to a node
  • You are attempting to append a node to a text node.
  • Your HTML is invalid (e.g. failing to close your target node)
  • The browser thinks the HTML you are attempting to append is XML (fix by adding <!doctype html> to your injected HTML, or specifying the content type when fetching via XHR)

Solution 2 - Javascript

If you are getting this error due to a jquery ajax call $.ajax

Then you may need to specify what the dataType is coming back from the server. I have fixed the response a lot using this simple property.

$.ajax({
    url: "URL_HERE",
    dataType: "html",
    success: function(response) {
        $('#ELEMENT').html(response);
    }
});

Solution 3 - Javascript

Specifically with jQuery you can run into this issue if forget the carets around the html tag when creating elements:

 $("#target").append($("div").text("Test"));

Will raise this error because what you meant was

 $("#target").append($("<div>").text("Test"));

Solution 4 - Javascript

This error can occur when you try to insert a node into the DOM which is invalid HTML, which can be something as subtle as an incorrect attribute, for example:

// <input> can have a 'type' attribute
var $input = $('<input/>').attr('type', 'text');
$holder.append($input);  // OK

// <div> CANNOT have a 'type' attribute
var $div = $('<div></div>').attr('type', 'text');
$holder.append($div);   // Error: HIERARCHY_REQUEST_ERR: DOM Exception 3

Solution 5 - Javascript

@Kelly Norton is right in his answer that The browser thinks the HTML you are attempting to append is XML and suggests specifying the content type when fetching via XHR.

It's true however you sometimes use third party libraries that you are not going to modify. It's JQuery UI in my case. Then you should provide the right Content-Type in the response instead of overriding the response type on JavaScript side. Set your Content-Type to text/html and you are fine.

In my case, it was as easy as renaming the file.xhtml to file.html - application server had some extension to MIME types mappings out of the box. When content is dynamic, you can set the content type of response somehow (e.g. res.setContentType("text/html") in Servlet API).

Solution 6 - Javascript

You can see these questions

https://stackoverflow.com/questions/2934049/getting-hierarchy-request-err-when-using-javascript-to-recursively-generate-a-nes

or

https://stackoverflow.com/questions/757232/jquery-ui-dialog-with-asp-net-button-postback

The conclusion is

when you try to use function append, you should use new variable, like this example

jQuery(function() {
   var dlg = jQuery("#dialog").dialog({ 
                        draggable: true, 
                        resizable: true, 
                        show: 'Transfer', 
                        hide: 'Transfer', 
                        width: 320, 
                        autoOpen: false, 
                        minHeight: 10, 
                        minwidth: 10 
          });
  dlg.parent().appendTo(jQuery("form:first"));
});

In the example above, uses the var "dlg" to run the function appendTo. Then error “HIERARCHY_REQUEST_ERR" will not come out again.

Solution 7 - Javascript

I encountered this error when using the Google Chrome extension Sidewiki. Disabling it resolved the issue for me.

Solution 8 - Javascript

I'm going to add one more specific answer here because it was a 2 hour search for the answer...

I was trying to inject a tag into a document. The html was like this:

<map id='imageMap' name='imageMap'>
  <area shape='circle' coords='55,28,5' href='#' title='1687.01 - 0 percentile' />
</map>

If you notice, the tag is closed in the preceding example (<area/>). This was not accepted in Chrome browsers. w3schools seems to think it should be closed, and I could not find the official spec on this tag, but it sure doesn't work in Chrome. Firefox will not accept it with <area/> or <area></area> or <area>. Chrome must have <area>. IE accepts anything.

Anyway, this error can be because your HTML is not correct.

Solution 9 - Javascript

I know this thread is old, but I've encountered another cause of the problem which others might find helpful. I was getting the error with Google Analytics trying to append itself to an HTML comment. The offending code:

document.documentElement.firstChild.appendChild(ga);

This was causing the error because my first element was an HTML comment (namely a Dreamweaver template code).

<!-- #BeginTemplate "/Templates/default.dwt.php" -->

I modified the offending code to something admittedly not bulletproof, but better:

document.documentElement.firstChild.nodeType===1 ? document.documentElement.firstChild.appendChild(ga) : document.documentElement.lastChild.appendChild(ga);

Solution 10 - Javascript

If you run into this problem while trying to append a node into another window in Internet Explorer, try using the HTML inside the node instead of the node itself.

myElement.appendChild(myNode.html());

IE doesn't support appending nodes to another window.

Solution 11 - Javascript

This ERROR happened to me in IE9 when I tried to appendChild an dynamically to a

which already existed in a window A. Window A would create a child window B. In window B after some user action a function would run and do an appendChild on the form element in window A using window.opener.document.getElementById('formElement').appendChild(input);

This would throw an error. Same with creating the input element using document.createElement('input'); in the child window, passing it as a parameter to the window.opener window A, and there do the append. Only if I created the input element in the same window where I was going to append it, it would succeed without errors.

Thus my conclusion (please verify): no element can be dynamically created (using document.createElement) in one window, and then appended (using .appendChild) to an element in another window (without taking maybe a particular extra step I missed to ensure it is not considered XML or something). This fails in IE9 and throws the error, in FF this works fine though.

PS. I don't use jQuery.

Solution 12 - Javascript

I got that error because I forgot to clone my element.

// creates an error
clone = $("#thing");
clone.appendTo("#somediv");

// does not
clone = $("#thing").clone();
clone.appendTo("#somediv");

Solution 13 - Javascript

Another reason this can come up is that you are appending before the element is ready e.g.

<body>

<script>
document.body.appendChild(foo);
</script>

</body>
</html>

In this case, you'll need to move the script after the . Not entirely sure if that's kosher, but moving the script after the body doesn't seem to help :/

Instead of moving the script, you can also do the appending in an event handler.

Solution 14 - Javascript

Just for reference.

IE will block appending any element created in a different window context from the window context that the element is being appending to.

e.g

var childWindow = window.open('somepage.html');

//will throw the exception in IE
childWindow.document.body.appendChild(document.createElement('div'));

//will not throw exception in IE
childWindow.document.body.appendChild(childWindow.document.createElement('div'));

I haven't figured out how to create a dom element with jQuery using a different window context yet.

Solution 15 - Javascript

I get this error in IE9 if I had disabled script debugging (Internet Explorer) option. If I enable script debugging I don't see the error and the page works fine. This seems odd what is the DOM exception got to do with debugging either enabled or disabled.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJulian WeimerView Question on Stackoverflow
Solution 1 - JavascriptKelly NortonView Answer on Stackoverflow
Solution 2 - JavascriptDave RobertsonView Answer on Stackoverflow
Solution 3 - JavascriptFatmuemooView Answer on Stackoverflow
Solution 4 - JavascriptAndrew MacLeodView Answer on Stackoverflow
Solution 5 - JavascriptNowakerView Answer on Stackoverflow
Solution 6 - JavascriptpureView Answer on Stackoverflow
Solution 7 - JavascriptJesse FowlerView Answer on Stackoverflow
Solution 8 - JavascriptmarkthegreaView Answer on Stackoverflow
Solution 9 - JavascriptS.WalkerView Answer on Stackoverflow
Solution 10 - JavascriptDan-NolanView Answer on Stackoverflow
Solution 11 - JavascriptYetiView Answer on Stackoverflow
Solution 12 - Javascriptcode_monkView Answer on Stackoverflow
Solution 13 - JavascriptallyourcodeView Answer on Stackoverflow
Solution 14 - JavascriptT.HoView Answer on Stackoverflow
Solution 15 - JavascriptAnnapoorni DView Answer on Stackoverflow