Question

I have a string such as <html><body><div id="message">Hello World!</div></body></html> and I would like to get the content of the #message element without parsing the HTML myself.

I thought maybe I could create a document object from a string in Gecko (this is for a Firefox add on) but I don't see any simple way.

I noticed that there is a createDocument method, but that doesn't take a string. I'd have to strip the <html> portion from the text, and then again I'm starting to assume stuff.

Anyone have any ideas? Thanks.

EDIT: This seems to work for me:

doc = document.implementation.createDocument( "http://www.w3.org/1999/xhtml", "html", null );
doc.firstChild.innerHTML = '<html><body><div id="message">Hello World!</div></body></html>';
node = doc.getElementById( "message" ); 
alert( node.innerHTML );
Was it helpful?

Solution

Where do you get the string from? If it's XML, you could get away with using DOMParser.

Otherwise, you have to create an HTML document - https://developer.mozilla.org/en/Parsing_HTML_From_Chrome.

The fact that just using createDocument works seems suspicious, because people used more complicated solutions all this time.

OTHER TIPS

Don't like answering my own question, but this seems to have worked for me:

doc = document.implementation.createDocument( "http://www.w3.org/1999/xhtml", "html", null );
doc.firstChild.innerHTML = '<html><body><div id="message">Hello World!</div></body></html>';
node = doc.getElementById( "message" ); 
alert( node.innerHTML );

Is it always that syntax? If so, why not use a regex?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top