views:

425

answers:

3

I have a var that contains a full html page, including the head, html, body, etc. When I pass that string into the .html() function, jQuery strips out all those elements, such as body, html, head, etc, which I don't want.

My data var contains:

<html>
<head>
<title>Untitled Document</title>
</head>
<body>
</body>
</html>

Then my jQuery is:

// data is a full html document string
data = $('<div/>').html(data);
// jQuery stips my document string!
alert(data.find('head').html());

I am needing to manipulate a full html page string, so that I can return what is in the element. I would like to do this with jQuery, but it seems all of the methods, append(), prepend() and html() all try to convert the string to dom elements, which remove all the other parts of a full html page.

Is there another way that I could do this? I would be fine using another method. My final goal is to find certain elements inside my string, so I figured jQuery would be best, since I am so used to it. But, if it is going to trim and remove parts of my string, I am going to have to look for another method.

Ideas?

A: 

There is no need for the container div.

Have you tried this?:

var foo = $(data);  // data is your full html document string

Then you can search inside of it like so:

$('.someClass', foo); // foo is the document you created earlier

Update:

As another answered mentioned, how this will act comes down to the browser.

I looked at the jQuery docs a bit and found this:

When the HTML is more complex than a single tag without attributes, as it is in the above example, the actual creation of the elements is handled by the browser's innerHTML mechanism. Specifically, jQuery creates a new <div> element and sets the innerHTML property of the element to the HTML snippet that was passed in.

So it seems that when you are using a whole html doc as a string, it's no different than setting the innerHTML property of a div you make using createElement.

TM
`$('<html><head></head></html>')` raises a dozen JS errors, at least in Chrome.
Max Shawabkeh
That does not seem to work. I tried alerting $('head', data).html() but that just returns null.
Nic Hubbard
@Max for me it doesn't give any errors, I just get an empty object back (in Chrome). That is odd, I had tested `$('<html>')` which works just fine.
TM
+6  A: 

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.

jitter
Thanks for investigating this, I appreciate the thorough look at this. I think my solution to this might be a regex that can look through my string for a specific element.
Nic Hubbard
+2  A: 

Nope, the jQuery html function is just sending the string through to the element's innerHTML property, which is a function of the browser that tells it to parse the HTML into DOM elements and add them to the page.

Your browser doesn't work with a page as HTML data, it works with it as DOM and imports/exports HTML.

JavaScript has very good Regular Expression support. Depending on the complexity of your task, you may find this is the best way to process your data.

Tobias Cohen