PDA

View Full Version : I am just playing around...but



Bartfast
02-24-2006, 04:18 PM
Ok, I am no programmer and am just playing around. Kind of a hobby.
Anyway, I have this script and I am wondering why the <td align=center/> has no effect on the item placed in the cell? The first <td align=center> in pure HTML works fine but not inside the script (seen lower in red). The 'count' still aligns to the left.

Thanks


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>

<div align="center">
<table border=1>
<tr>
<td width=200 align=center>
hi
</td>
</tr>
</table>
<table>
<tr>
<td>
<input type="button" value="push me" onclick="a()">
</td>
</tr>
</table>

<script>
function a(){
var count = 0;
while (count<11)
{
document.write("<table align=center width=50% border=1/><tr/> <td align=center/>"+count+"</td></tr/></table/><br/>");
count++;
}
document.write("Loop done!");
}
</script>

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

Twey
02-24-2006, 04:33 PM
Firstly, attribute values must be quoted (so that's align="center" or even align=\"center\" being as you're using Javascript) unless they're pure number values (no units or funny business), and even then it doesn't hurt. Secondly, it's a recommendation that you leave a space before the / of a self-closing tag to seperate it from the last attribute.
Try:
document.write("<table align=\"center\" width=\"50%\" border=1 /><tr><td align=\"center\" />"+count+"</td></tr></table><br />");

Nothing to do with your problem, but I should point out that every single attribute you've applied to that table and td has been superseded by CSS.

HTML CSS
width="50%" width: 50%;
align="center" text-align: center;
border=1 border: solid 1px;

Bartfast
02-24-2006, 05:06 PM
Thanks that worked great!
As to the CSS, yes I know (sort of) I use CSS on my web site.
I am just now trying to learn some JavaScript and am working out of the book "The Complete Reference JavaScript 2nd ed". More for fun than anything. And I am taking some of their examples and making them do more in an attempt to really understand what is going on.

Thanks again!
P.S. I am sure I will be back with more questions as I go through the book. I hope my questions are not too simple and help others also.

Twey
02-24-2006, 05:11 PM
I am taking some of their examples and making them do more in an attempt to really understand what is going on.Best way to learn a language, short of launching into a project.

jscheuer1
02-24-2006, 05:28 PM
Too late, I know but, why would you ever need to self close a td tag?

Twey
02-24-2006, 05:51 PM
Woah... I should have noticed that. Probably about the same time I saw </tr/>. :) Bartfast: I think you missed the point of self-closing tags. They're used for tags that don't have an ending tag.

mwinter
02-24-2006, 08:36 PM
Too late, I know but, why would you ever need to self close a td tag?If we were considering XML, or an application of it, then it's quite legitimate to replace any occurances of:



<tag></tag>
with:



<tag/>
no matter what tag may be.

However, we are not considering XML. The OP is using HTML and empty-element (a.k.a. self-closing or minimized start-) tags do not exist. A forward slash should only occur in tags within a (quoted) attribute value or as part of the end tag[1].

Mike


[1] If HTML was parsed as an application of SGML this statement wouldn't be applicable, though it would be a good idea as NET are obscure.

Bartfast
02-24-2006, 09:03 PM
Woah... I should have noticed that. Probably about the same time I saw </tr/>. :) Bartfast: I think you missed the point of self-closing tags. They're used for tags that don't have an ending tag.

Well, I think I get it now. Thanks. This book has not address 'self-closing tags' as of yet. I was just jumping in with one of their examples that had one in it without explicitly explaining what ‘it’ was, but it worked for their example. But now I see just what they are used for with your explanation.
Sorry for being such a neophyte with all of this but when you are taking the 'self taught' approach that is what you get. You know, I know just enough to be dangerous!:D
More dangerous questions to come so watch out!

Thanks to all of you for your help and patience!