Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > HTMLDocument object from xmlHttpRequest

Reply
Thread Tools

HTMLDocument object from xmlHttpRequest

 
 
Jeff
Guest
Posts: n/a
 
      03-11-2007
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

 
Reply With Quote
 
 
 
 
Danny
Guest
Posts: n/a
 
      03-11-2007
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
 
Reply With Quote
 
 
 
 
Jeff
Guest
Posts: n/a
 
      03-11-2007
On Mar 10, 7:03 pm, Danny <(E-Mail Removed)> wrote:
> 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

>
> Danny



 
Reply With Quote
 
Danny
Guest
Posts: n/a
 
      03-11-2007
Jeff wrote:

> On Mar 10, 7:03 pm, Danny <(E-Mail Removed)> wrote:
>> 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
>
>>
>> Danny



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
 
Reply With Quote
 
Jeff
Guest
Posts: n/a
 
      03-11-2007
On Mar 10, 7:28 pm, Danny <(E-Mail Removed)> wrote:
> Jeff wrote:
> > On Mar 10, 7:03 pm, Danny <(E-Mail Removed)> wrote:
> >>http://www.quirksmode.org/dom/importxml.htmltograb 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

>
> >> Danny

>
> 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

 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      03-11-2007
Jeff wrote:

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




--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to create MSHTML.HTMLDocument COM object Steve ASP General 0 10-27-2006 03:10 PM
HTMLDocument and Xpath swilson@acs.on.ca Python 3 02-07-2006 09:59 AM
HTMLDocument getElement fails El Zorro Java 0 08-05-2005 07:59 PM
How to use custom tag in HTMLDocument in swings JTextPane ? T. Sander Java 0 07-12-2004 10:10 AM
String into HTMLDocument Ike Java 1 04-05-2004 07:38 PM



Advertisments