HTMLDocument object from xmlHttpRequest

J

Jeff

Is there a standard way of getting the HTMLDocument object
representation of a remote page using Javascript? If I request an
HTML page, the xmlHttpRequest returns either text or an XMLDocument.
I can't figure out how to convert the former into an HTMLDocument
object and the latter doesn't seem to work if the page isn't valid
XHTML.

If that approach isn't going to work, I've envisioned hacks involving
loading a page in an invisible iframe instead of using xmlHttpRequest,
but didn't want to rehack the wheel.

Also, this is for a Firefox extension, so I really only need something
that will work with Firefox, although it would be cool to find out how
to do this generally.

Thanks!
Jeff
 
D

Danny

http://www.quirksmode.org/dom/importxml.html to grab the XML nodes and their
content into the document markup by means of DOM by using the .responseXML, as
for inserting it from .responseText property, just use .innerHTML property, be
keen that .innerHTML can be have issues since it calls on the html parser to
parse the markup and sometimes when you try to access its childNodes in DOM,
they may go astray. If you do not need to crawl its childNodes,
then .innerHTML works fine.

Danny
 
J

Jeff

http://www.quirksmode.org/dom/importxml.htmlto grab the XML nodes and their
content into the document markup by means of DOM by using the .responseXML, as
for inserting it from .responseText property, just use .innerHTML property, be
keen that .innerHTML can be have issues since it calls on the html parser to
parse the markup and sometimes when you try to access its childNodes in DOM,
they may go astray. If you do not need to crawl its childNodes,
then .innerHTML works fine.

Thanks for the suggestions.

I want to do the later, but it's not exactly straightforward from
there. First, I need to somehow create a HTMLDocument, then I'm
guessing insert an HTMLElement node, then remove the <html> and </
html> tags from the responseText, then set the innerHTML of the
HTMLElement to that text.

If I do that, will XPATH queries work on the resulting document? I'm
happy even if it only works on Firefox 2+.

Thanks,
Jeff
 
D

Danny

Jeff said:
Thanks for the suggestions.

I want to do the later, but it's not exactly straightforward from
there. First, I need to somehow create a HTMLDocument, then I'm
guessing insert an HTMLElement node, then remove the <html> and </
html> tags from the responseText, then set the innerHTML of the
HTMLElement to that text.

If I do that, will XPATH queries work on the resulting document? I'm
happy even if it only works on Firefox 2+.

Thanks,
Jeff


mydiv=document.createElement('div');
mymarkup=OBJ.responseText.replace(/<\/*(html|head|body)>/i,''); // what would
there be an <html> in the fragment markup, it should just be markup to be
inserted in a page's body

mydiv.innerHTML=mymarkup;
document.body.appendChild(mydiv);
if atop of the page
document.body.InsertBefore(mydiv, document.body.firstChild);

Danny
 
J

Jeff

mydiv=document.createElement('div');
mymarkup=OBJ.responseText.replace(/<\/*(html|head|body)>/i,''); // what would
there be an <html> in the fragment markup, it should just be markup to be
inserted in a page's body

mydiv.innerHTML=mymarkup;
document.body.appendChild(mydiv);
if atop of the page
document.body.InsertBefore(mydiv, document.body.firstChild);

Well, for instance if I request a URL, say http://www.yahoo.com/ and
to easily select elements of its DOM (It's a Greasemonkey script, so
the cross-site restrictions don't apply). So, I need a way of
creating a HTMLDocument and then initializing it. I assume I can do
something similar to the code you've provided, but it won't be exactly
like that.

-Jeff
 
M

Martin Honnen

Jeff said:
If that approach isn't going to work, I've envisioned hacks involving
loading a page in an invisible iframe instead of using xmlHttpRequest,
but didn't want to rehack the wheel.

Also, this is for a Firefox extension, so I really only need something
that will work with Firefox, although it would be cool to find out how
to do this generally.

With Mozilla, you need an invisible frame to parse a text/html document
into a DOM HTMLDocument. There is so far no API to parse a complete
text/html document into a DOM tree.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top