Web Analytics Made Easy -
StatCounter Show text as formated - CodingForum

Announcement

Collapse
No announcement yet.

Show text as formated

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Show text as formated

    Hi,

    How can we show text as formated onto a web page.

    Say the text comes from a Database or you create a few paragrahs in a variable. Now you want to display this on the page as formated. But when I output it, all hard returns, etc. have been removed.

    What can I use?

    I tried <pre></pre> but this makes the text run off the page cause like in a Word document, it would auto wrap, not with <pre>.

    Any ideas?


  • #2
    Here's some sample

    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#FFFFFF" text="#000000">
    <table width="200" border="1">
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>
    <script>

    var t = "This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text \n\n and more text...";

    document.write("<pre>"+t+"</pre>");

    </script>


    </td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </table>
    </body>
    </html>

    Comment


    • #3
      apply overflow: scroll; or overflow: hidden; style (depending on what behaviour you would like to have) to the element containing the text.
      Vladdy | KL
      "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

      Comment


      • #4
        Might try a new String method:

        <html>
        <head>
        <title>Untitled Document</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <script type="text/javascript">

        String.prototype.setHTMLbreaks = function() {
        return this.replace(/\n|\r/g, '<br>');
        }

        </script>
        </head>
        <body bgcolor="#FFFFFF" text="#000000">
        <table width="100%" border="1">
        <tr>
        <td></td><td></td><td></td>
        </tr>
        <tr>
        <td></td><td>
        <script type="text/javascript">

        var t = "This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text This is some text \n\n and more text...";

        document.write(t.setHTMLbreaks());

        </script>
        </td><td></td>
        </tr>
        <tr>
        <td></td><td></td><td></td>
        </tr>
        </table>
        </body>
        </html>

        Comment

        Working...
        X