Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    why is the \t now working?

    I think that if there is more space between the columns that it would be easier to read, but whatever I try is not working. Just putting tabs between them is my latest idea, however, the space is not all that big. Is it possible that I have entered it wrong?

    Thank you, the code is below:

    <html>


    <head>
    <title>Exercise</title>
    <script type = "text/javascript">
    <!--
    var row = 0;
    var cols = 0;
    var sum = (row + 1);

    if (row ==0)
    {
    document.write("sum &nbsp square &nbsp cube");
    document.write("<br>");
    row++;
    }

    while((row >= 0) && (row <= 11))
    {
    document.write(row + "\t" + (row * row) + "\t" + (row * row * row));
    document.write("<br>");
    row++;
    }


    // -->
    </script>
    </head>
    <body>
    <p>what? seriously, this is working?</p>
    </body>

    </html>

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    HTML treats any whitespace between content as if it were a single space. A tab is whitespace and so HTML collapses it with any adjacent whitespace and displays them all as a single space.

    That looks like what you are trying to create is a table - see http://javascriptexample.net/domtable12.php for an example of how to create a table of information using JavaScript.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    If you alert your string, you will see the tab works fine, but html treats mutiple white spaces as a single space.

    The tab appears in the alert() output.

    Code:
                    var str = row + "\t" + (row * row) + "\t" + (row * row * row);
                    
                    alert(str);
    [ot]

    For a known number of iterations you normally use a FOR loop and not a WHILE loop.

    WHILE loops are normally used when the number of iterations is unknown.

    You could put your IF and WHILE blocks into a single FOR loop and you wouldn't need the WHILE loop at all.


    [/ot]
    Last edited by webdev1958; 03-09-2012 at 02:08 AM.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Enclose the text in <pre></pre> tags to preserve whitespace.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Quote Originally Posted by glenngv View Post
    Enclose the text in <pre></pre> tags to preserve whitespace.
    It is better to place tabular data inside a table rather than in pre tags.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by felgall View Post
    It is better to place tabular data inside a table rather than in pre tags.
    I know, I was just showing a way how to preserve whitespace in HTML.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    The other disadvantage of using <pre> tags is that you MUST use a fixed width font for the content if you want things to always line up. If you use a proportional width font then the width of the whitespace can change depending on the adjacent text.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •