Converting string content to html

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

    #1

    Converting string content to html

    Hi all.

    Well, I need some light in this simple thing I'm trying to do. I'm
    using the XMLHttpRequest to retrieve some data from a db via php
    script. The result is passed to a "results" array of strings, which
    contain the data from the script.

    But, the data from the script, which are the content of the strings,
    have html tags in it. The thing is: I'm using the DOM to append the
    string result to a table already built in the page (using
    create_element and appendchild).

    insertO = document.getEle mentById("outpu t_table");
    oTR = document.create Element('tr');
    oTD = document.create Element('td');
    oText = document.create TextNode(Text);
    oTD.appendChild (oText);
    oTR.appendChild (oTD);
    insertO.tBodies[0].appendChild(oT R);

    The Text var would contain something like "<font color="red">thi s
    <b>is</b> html</font>".

    This way the output isn't parsed, i.e., it shows the tags.
    Does anyone have an idea on how to overcome this?

    Thks
    Alex

  • unruly

    #2
    Re: Converting string content to html

    whoops, I guess I slightly misread your post. You'd still want to do a
    regexp replace by using this:

    /<[^<>]*>//ig as the expression.

    Comment

    • unruly

      #3
      Re: Converting string content to html

      You could just do a regexp replace for < and > for their HTML character
      equivilents, &lt; and &gt; respectivly.

      Comment

      • RobB

        #4
        Re: Converting string content to html

        Alex wrote:[color=blue]
        > Hi all.
        >
        > Well, I need some light in this simple thing I'm trying to do. I'm
        > using the XMLHttpRequest to retrieve some data from a db via php
        > script. The result is passed to a "results" array of strings, which
        > contain the data from the script.
        >
        > But, the data from the script, which are the content of the strings,
        > have html tags in it. The thing is: I'm using the DOM to append the
        > string result to a table already built in the page (using
        > create_element and appendchild).
        >
        > insertO = document.getEle mentById("outpu t_table");
        > oTR = document.create Element('tr');
        > oTD = document.create Element('td');
        > oText = document.create TextNode(Text);
        > oTD.appendChild (oText);
        > oTR.appendChild (oTD);
        > insertO.tBodies[0].appendChild(oT R);
        >
        > The Text var would contain something like "<font color="red">thi s
        > <b>is</b> html</font>".
        >
        > This way the output isn't parsed, i.e., it shows the tags.
        > Does anyone have an idea on how to overcome this?
        >
        > Thks
        > Alex[/color]

        Did you want the HTML to be parsed, or discarded? If it's the former,
        this might be a textbook case for the value of innerHTML....



        Comment

        • RobG

          #5
          Re: Converting string content to html

          RobB wrote:
          [...][color=blue]
          >
          > Did you want the HTML to be parsed, or discarded? If it's the former,
          > this might be a textbook case for the value of innerHTML....
          >
          > http://www.developer-x.com/content/innerhtml/
          >[/color]

          Or a textbook case for ensuring that the text object(s) were
          appropriately serialized in the XML in the first place. ;-)

          (I make no admission to knowing what that format might be!)

          Which requires the obvious response: supposing I wanted to use DOM to
          create text nodes for the above case, what is the appropriate format
          so that I can create appropriate DOM objects then give them
          appropriate attributes?

          Below is a feeble attempt to get a start on it, but I could not work
          out how to read styles from an object then apply them to an element.

          e.g. if I create a span 'oSpan' and put some text into it with a text
          node, then create say w='color' and y='red', the following does
          not seem to work:

          oSpan = document.create Element('span') ;
          oSpan.appendChi ld(document.cre ateTextNode('st uff'));
          oSpan.style.w = y;

          Then append oSpan to the document, 'stuff' is in the default
          color. If I explicitly use:

          oSpan.style.col or = 'red';
          or
          oSpan.style.col or = y;

          'stuff' is red.

          If there really is no way to do this? Do I need a case statement for
          every single style attribute I want to support? If so, why isn't
          innerHTML (or some similar method) available in DOM 3? Or is there
          and I'm just displaying my ignorance?


          <style type="text/css">
          ..rg {color:red;}
          </style>
          <script type="text/javascript">
          function addStuff(z){
          var tgt = document.getEle mentById(z);

          // obj should be created by parsing XML, save that for later...
          var obj = {};
          obj[0] = {text:'this ','color':'red' }
          obj[1] = {text:'is','fon tWeight':'bold' }
          obj[2] = {text:' html','fontWeig ht':'normal'}

          var i = 0;
          var oTxt, oSpan;

          do {
          for (w in obj[i]){
          switch (w) {

          case 'text':
          alert('Creating : ' + w + ' \'' + obj[i][w] + '\'');
          oSpan = document.create Element('span') ;
          oTxt = document.create TextNode(obj[i][w]);
          oSpan.appendChi ld(oTxt);
          break;

          default:
          alert('Setting style: ' + w + ' to \'' + obj[i][w] + '\'');
          // oSpan.style.w = obj[i][w];
          // oSpan.style.w = '\'' + obj[i][w] + '\'';
          // oSpan.style.fon tWeight = 'bold';
          oSpan.style.col or = obj[i][w];
          }
          }
          alert('adding ' + oSpan.nodeName) ;
          tgt.appendChild (oSpan);
          }
          while ( obj[++i] )
          }
          </script>
          <input type="button" onclick="addStu ff('xx')" value="addStuff ">
          <br>
          <span id="xx"></span><br>
          <span class="rg">samp le text</span>








          --
          Rob

          Comment

          • Richard Cornford

            #6
            Re: Converting string content to html

            RobG wrote:
            <snip>[color=blue]
            > e.g. if I create a span 'oSpan' and put some text into
            > it with a text node, then create say w='color'
            > and y='red', the following does not seem to work:
            >
            > oSpan = document.create Element('span') ;
            > oSpan.appendChi ld(document.cre ateTextNode('st uff'));
            > oSpan.style.w = y;
            >
            > Then append oSpan to the document, 'stuff' is in the default
            > color.[/color]

            That gives you a property of the style object called 'w' to which the
            string value 'red' has been assigned. There is no reason to expect a 'w'
            property of a style object to affect the presentation of the span.
            [color=blue]
            > If I explicitly use:
            >
            > oSpan.style.col or = 'red';
            > or
            > oSpan.style.col or = y;
            >
            > 'stuff' is red.
            >
            > If there really is no way to do this? ...[/color]
            <snip>

            oSpan.style[w] = y;

            Richard.


            Comment

            • RobG

              #7
              Re: Converting string content to html

              Richard Cornford wrote:[color=blue]
              > RobG wrote:
              > <snip>
              >[color=green]
              >>e.g. if I create a span 'oSpan' and put some text into
              >> it with a text node, then create say w='color'
              >> and y='red', the following does not seem to work:
              >>
              >> oSpan = document.create Element('span') ;
              >> oSpan.appendChi ld(document.cre ateTextNode('st uff'));
              >> oSpan.style.w = y;
              >>[/color][/color]
              [...][color=blue]
              >
              > oSpan.style[w] = y;
              >[/color]

              Dang - thanks!

              But I still find accessing properties this way a bit confusing.


              --
              Rob

              Comment

              Working...