xmlhttprequest question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • spark

    xmlhttprequest question

    I have been experimenting with xmlhttprequest. (I am a javascript
    newbie, but familiar with programming with VB, Perl, and some PHP.) I
    am trying to see if it's possible to receive html snippets back from a
    request, and then insert that snippet into the target page; for example,
    getting back a set of <ul> lists within lists, or a set of <option>
    tags, which could then be added to a page's extent lists, or to select
    inputs.

    I'm trying to do this by setting the content-type header in the returned
    data to text/xml, and I'm able to insert the nodes into the target page;
    but then they will not be formatted with any css page I may have set
    within the target page. Taking the aforementioned <ul> list, if the
    target page references a css page with some layout rules for ul, the
    inserted <ul> nodes aren't formatted using these rules.

    Here's my code:

    if (xmlhttp) {
    xmlhttp.open("G ET", "[command]",true);
    xmlhttp.onready statechange=fun ction() {
    if (xmlhttp.readyS tate==4) {
    var insertHere = document.getEle mentById('write root');
    var respDoc = xmlhttp.respons eXML;
    insertHere.pare ntNode.insertBe fore(respDoc.ch ildNodes
    [0].cloneNode(true ),insertHere);
    }
    }
    }

    If anyone has any suggestions for a different way to do this, I'd be
    very grateful. (I'm trying to do it this simply, that is, just insert
    nodes from a request result into the target page. My hope is to avoid
    writing extra list-parsing logic on the client side of my app.)

  • Martin Honnen

    #2
    Re: xmlhttprequest question



    spark wrote:
    [color=blue]
    > I have been experimenting with xmlhttprequest. (I am a javascript
    > newbie, but familiar with programming with VB, Perl, and some PHP.) I
    > am trying to see if it's possible to receive html snippets back from a
    > request, and then insert that snippet into the target page; for example,
    > getting back a set of <ul> lists within lists, or a set of <option>
    > tags, which could then be added to a page's extent lists, or to select
    > inputs.
    >
    > I'm trying to do this by setting the content-type header in the returned
    > data to text/xml, and I'm able to insert the nodes into the target page;
    > but then they will not be formatted with any css page I may have set
    > within the target page. Taking the aforementioned <ul> list, if the
    > target page references a css page with some layout rules for ul, the
    > inserted <ul> nodes aren't formatted using these rules.[/color]

    Make sure you send elements in the XHTML namespace e.g.
    <ul xmlns="http://www.w3.org/1999/xhtml">
    <li>Kibo</li>
    </ul>
    then with Mozilla you can do:

    [color=blue]
    > if (xmlhttp) {
    > xmlhttp.open("G ET", "[command]",true);
    > xmlhttp.onready statechange=fun ction() {
    > if (xmlhttp.readyS tate==4) {
    > var insertHere = document.getEle mentById('write root');
    > var respDoc = xmlhttp.respons eXML;
    > insertHere.pare ntNode.insertBe fore(respDoc.ch ildNodes
    > [0].cloneNode(true ),insertHere);[/color]

    insertHere.pare ntNode.insertBe fore(
    insertHere.owne rDocument.impor tNode(respDoc.d ocumentElement, true),
    insertHere
    );

    That should then also work with other browsers having a common DOM
    implementation for HTML and XML but with IE/Win that approach is not
    possible, IE doesn't have an XML DOM implementation but uses MSXML to
    parse XML and you can't move nodes from the MSXML XML DOM implementation
    to the IE HTML DOM implementation. So with IE you are left to using
    responseText and insert that e.g.
    someElement.inn erHTML = xmlhttp.respons eText;
    or
    someElement.ins ertAdjacentHTML ('beforeEnd', xmlhttp.respons eText);
    or you would need to write importNode for IE e.g. walk the MSXML XML DOM
    and create all nodes as needed in the IE HTML DOM. But for instance the
    Sarissa project suggests that IE with innerHTML performs better than an
    attempt to implement importNode with DOM createXXX method calls so there
    importNode is "implemente d" by creating a div, setting its innerHTML and
    then using the child nodes.

    The Sarissa project is here:
    <http://sarissa.sourcef orge.net/>


    --

    Martin Honnen

    Comment

    • spark

      #3
      Re: xmlhttprequest question

      In article <4218e06d$0$249 40$9b4e6d93@new sread2.arcor-online.net>,
      mahotrash@yahoo .de says...[color=blue]
      >
      > Make sure you send elements in the XHTML namespace e.g.
      > <ul xmlns="http://www.w3.org/1999/xhtml">
      > <li>Kibo</li>
      > </ul>
      > then with Mozilla you can do:
      >
      >[/color]
      [example code snipped][color=blue]
      >
      > That should then also work with other browsers having a common DOM
      > implementation for HTML and XML but with IE/Win that approach is not
      > possible, IE doesn't have an XML DOM implementation but uses MSXML to
      > parse XML and you can't move nodes from the MSXML XML DOM implementation
      > to the IE HTML DOM implementation.[/color]

      Thanks Martin!!

      Actually I'm not too concerned (at the moment at least) about IE, as the
      app I'm writing will not be available on a public webpage, and so I'm
      focusing my development on firefox only.

      I have an additional question. I put the namespace declaration into my
      returned doc, and now the ul list, and the ul lists within it, are all
      formatted as html. However, this particular ul list was set up in my
      css sheet as _drop down menus_ - in other words, the top (or root) ul
      menu is displayed horizontally, and all ul list below the top are
      invisible ( display: none; ) until the user hovers the cursor over an
      item in the top list; and a few other effects. And this additional css
      stuff is missed entirely. So it appears that the namespace has some
      basic formatting commands, but not others. I tried placing a reference
      to my stylesheet into the returned xml doc ( <?xml-stylesheet .. ?> ),
      but that doesn't seem to work. Are there different namespaces I can
      include that might give me the desired formatting? Is there a webpage
      where I can view available public namespaces? Or is there a better way
      to refer to my stylesheet? Or would I have to recreate my stylesheet as
      an XML schema of some kind (I see on http://www.w3.org/TR/REC-xml-names/
      that sometimes a namespace is declared via reference to a schema?)?

      Thanks again!

      Comment

      • spark

        #4
        Re: xmlhttprequest question

        In article <MPG.1c82b6e832 faa2c989681@new s-central.giganew s.com>,
        no@no.no says...[color=blue]
        > In article <4218e06d$0$249 40$9b4e6d93@new sread2.arcor-online.net>,
        > mahotrash@yahoo .de says...[color=green]
        > >
        > > Make sure you send elements in the XHTML namespace e.g.
        > > <ul xmlns="http://www.w3.org/1999/xhtml">
        > > <li>Kibo</li>
        > > </ul>
        > > then with Mozilla you can do:
        > >
        > >[/color]
        > [example code snipped][color=green]
        > >
        > > That should then also work with other browsers having a common DOM
        > > implementation for HTML and XML but with IE/Win that approach is not
        > > possible, IE doesn't have an XML DOM implementation but uses MSXML to
        > > parse XML and you can't move nodes from the MSXML XML DOM implementation
        > > to the IE HTML DOM implementation.[/color]
        >
        >
        > I have an additional question.[/color]
        [snip][color=blue]
        > So it appears that the namespace has some
        > basic formatting commands, but not others.[/color]

        You know what, I goofed. I had no reference in my experimental document
        to the css sheet I was talking about. Once I put that reference in, the
        inserted xml/html doc was formatted correctly.

        So - thank again Martin!

        Comment

        Working...