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

15

196

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?

Lanthanum answered 10/8, 2009 at 18:19 Comment(0)
T
242

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)
Therese answered 11/8, 2009 at 3:58 Comment(5)
The error also occurs when you try and append a node to itself. I just ran into this one myself :-)Flatways
I just got this trying to add a null to an element (and a totally unrelated tip - always make sure your functions return the thing you wrote them to return :P)Tirzah
I saw this when one of the closing tags was missing out of the target.Stakhanovism
IE(9) may throw this error when using jQuery to append AJAX results. Avoid this by using response.xml where available. For example, $(e).append(response.xml || $(response));Butterbur
Got this when I changed from jQuery (desktop) to zepto on Android. Switched back.Ashaashamed
A
5

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);
    }
});
Alewife answered 15/5, 2012 at 21:54 Comment(2)
single code base for web/phonegap, refactored to dynamically loading template using answer by koorchik at #8367233. worked in desktop browser, did not work in mobile browser or native app. this solution did the trick.Unrestraint
Same issue here, weirdly only in iOS 5. Specifying dataType worked. ThanksBessbessarabia
T
3

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"));
Thom answered 17/2, 2012 at 16:43 Comment(0)
M
3

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
Madson answered 1/4, 2012 at 13:45 Comment(0)
G
2

@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).

Glorygloryofthesnow answered 19/11, 2012 at 10:57 Comment(0)
M
1

You can see these questions

Getting HIERARCHY_REQUEST_ERR when using Javascript to recursively generate a nested list

or

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.

Meacham answered 30/5, 2011 at 9:42 Comment(0)
A
1

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

Anschauung answered 11/7, 2011 at 21:3 Comment(0)
M
1

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.

Monney answered 5/12, 2011 at 16:23 Comment(0)
M
1

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);
Mano answered 13/9, 2012 at 1:25 Comment(0)
C
1

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.

Ciel answered 29/10, 2012 at 20:54 Comment(1)
Did you actually try this? Even in Chrome i am getting TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.(…). Thats because .html() is of type string and appendChild is expecting type Node.Flexible
C
1

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.

Coeducation answered 13/1, 2013 at 23:36 Comment(1)
Thanks to your pointer, i figured out a way around this. Please see the answer I posted if you're interested interested.Petrie
H
0

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");
Herbage answered 20/3, 2012 at 20:47 Comment(0)
S
0

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.

Schulman answered 17/10, 2012 at 17:58 Comment(0)
P
0

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.

Petrie answered 8/2, 2013 at 2:25 Comment(0)
C
0

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.

Caresa answered 22/2, 2013 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.