CDATA - Error in a Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bobbya
    New Member
    • Nov 2007
    • 3

    #1

    CDATA - Error in a Javascript

    I am a newbie to javascript, I am trying to convert the numbers from 1 to 256 to there character code using the following code while executing IE7. I am getting a error on the CDATA tag.

    [CODE=javascript]<script type="text/javascript">
    <![CDATA[
    k=1;
    document.write( '<table>');
    for(i=1; i <16; i++) {
    document.write( '<tr>');
    for(j=0; j<16; j++) {
    document.write( '<tr>' + k + '<br />' + &#k; + '</tr>');
    }
    document.write( '</tr>);
    }
    document.write( </table>);
    ]]>
    </script>[/CODE]
    Last edited by gits; Nov 11 '07, 12:34 PM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    have a look at the comments in the code, that is fixed now:

    [CODE=javascript]<script type="text/javascript">
    <![CDATA[
    var k=1;
    document.write( '<table>');

    for(i=1; i <16; i++) {
    document.write( '<tr>');

    for(j=0; j<16; j++) {
    document.write( '<tr>' + k + '<br />' + &#k; + '</tr>');
    }

    // add quotes here
    document.write( '</tr>');
    }

    // add the quotes here too
    document.write( '</table>');
    ]]>
    </script>[/CODE]
    so you had simple syntax errors.

    kind regards

    Comment

    • Logician
      New Member
      • Feb 2007
      • 210

      #3
      Originally posted by bobbya
      I am a newbie to javascript, I am trying to convert the numbers from 1 to 256 to there character code using the following code while executing IE7. I am getting a error on the CDATA tag.
      Are you sure you have the right doctype and whatever else may be needed to use a CDATA island?
      There is another syntax error here:
      Code:
      document.write('<tr>' + k + '<br />' + &#k; + '</tr>');
      Should be:
      Code:
      document.write('<tr>' + k + '<br />&#' + k + ';</tr>');
      Your algorithm is still incorrect beacause k isn't incremented and the table structure is wrong.

      [CODE=javascript]document.write( '<table width=100%>');

      for(var i=0, k=0; i <16; i++)
      {
      document.write( '<tr bgcolor="#'+(i& 1?'00F0FF':'00C 0FF')+'">');

      for(var j=0; j<16; j++, k++)
      document.write( '<th width=3%>' + k + '&#' + k + ';');

      document.write( '</tr>');
      }

      document.write( '</table>');
      [/CODE]

      Comment

      • bobbya
        New Member
        • Nov 2007
        • 3

        #4
        Originally posted by gits
        hi ...

        have a look at the comments in the code, that is fixed now:

        [CODE=javascript]<script type="text/javascript">
        //<![CDATA[
        var k=1;
        document.write( '<table>');

        for(i=1; i <16; i++) {
        document.write( '<tr>');

        for(j=0; j<16; j++) {
        document.write( '<tr>' + k + '<br />&#' + k + '</tr>');
        k++;
        }

        // add quotes here
        document.write( '</tr>');
        }

        // add the quotes here too
        document.write( '</table>');
        //]]>
        </script>[/CODE]
        so you had simple syntax errors.

        kind regards

        Thanks...

        For your solution to the above problem..it now works

        Comment

        Working...