Does jQuery strip some html elements from a string when using .html()?

After a few quick tests it seems do me that this behavior isn’t caused by jQuery but instead by the browser.

As you can easily verify yourself (DEMO http://jsbin.com/ocupa3)

var data = "<html><head><title>Untitled Document</title></head><body><p>test</p></body></html>";
data = $('<div/>').html(data);
alert(data.html());

yields different results in different browsers

Opera 10.10

<HEAD><TITLE>Untitled Document</TITLE></HEAD><P>test</P>

FF 3.6

<title>Untitled Document</title><p>test</p>

IE6

<P>test</P>

so this has nothing to do with jQuery, It’s the browsers which strip some tags when you insert a whole html string inside a div. But you would need to step through the whole jQuery code for html() to be sure. And you would need to do that for all browsers as there are several different ways jQuery tries to do it’s job.


For a solution I advise you to investigate using an iframe (possibly hidden) and to set that iframe content to the html-string you have. But be aware that fiddling with iframes and changing their content programmatically isn’t an easy task either. There are also different browser related quirks and timing issues involved.

Leave a Comment