HTMLDocument object from xmlHttpRequest

Discussion in 'Javascript' started by Jeff, Mar 11, 2007.

  1. Jeff

    Jeff Guest

    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
    Jeff, Mar 11, 2007
    #1
    1. Advertising

  2. Jeff

    Danny Guest

    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
    Danny, Mar 11, 2007
    #2
    1. Advertising

  3. Jeff

    Jeff Guest

    On Mar 10, 7:03 pm, Danny <> 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
    Jeff, Mar 11, 2007
    #3
  4. Jeff

    Danny Guest

    Jeff wrote:

    > On Mar 10, 7:03 pm, Danny <> 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
    Danny, Mar 11, 2007
    #4
  5. Jeff

    Jeff Guest

    On Mar 10, 7:28 pm, Danny <> wrote:
    > Jeff wrote:
    > > On Mar 10, 7:03 pm, Danny <> 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
    Jeff, Mar 11, 2007
    #5
  6. 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/
    Martin Honnen, Mar 11, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ike

    String into HTMLDocument

    Ike, Apr 5, 2004, in forum: Java
    Replies:
    1
    Views:
    9,240
    Rhino
    Apr 5, 2004
  2. T. Sander
    Replies:
    0
    Views:
    3,805
    T. Sander
    Jul 12, 2004
  3. El Zorro

    HTMLDocument getElement fails

    El Zorro, Aug 5, 2005, in forum: Java
    Replies:
    0
    Views:
    518
    El Zorro
    Aug 5, 2005
  4. HTMLDocument and Xpath

    , Feb 3, 2006, in forum: Python
    Replies:
    3
    Views:
    702
  5. Steve
    Replies:
    0
    Views:
    489
    Steve
    Oct 27, 2006
Loading...

Share This Page