...

View Full Version : why is the \t now working?



hmcka
03-09-2012, 01:54 AM
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>

felgall
03-09-2012, 01:56 AM
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.

webdev1958
03-09-2012, 02:05 AM
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.



var str = row + "\t" + (row * row) + "\t" + (row * row * row);

alert(str);


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.

glenngv
03-09-2012, 07:05 AM
Enclose the text in <pre></pre> tags to preserve whitespace.

felgall
03-09-2012, 08:07 AM
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.

glenngv
03-09-2012, 08:27 AM
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.

felgall
03-09-2012, 07:31 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum