PDA

View Full Version : JavaScript and markup


David
28th Nov 2005, 03:05 pm
I learned something new today when I tested a file for XHTML 1.0 Strict validation.

The file contained a small script that had some embedded markup like so:
<script type="text/javascript">
document.writeln("<p class='day'>" + darr[now.getDay()] + "</p><p>"
+ now.getDate()
+ "<sup>" + narr[now.getDate()] + "</sup>"
+ " " + marr[now.getMonth()]
+ " " + now.getFullYear() + "</p>");
</script>

All this code does is to print the date and the paragraph tags are embedded within the script - it's a neat way of doing it because the whole thing is contained in a single script.

However, it appears that this is not valid XHTML 1.0 Strict - there should be no embedded tags within scripts. So, I had to change the code to this in order to comply:
<p class="day">
<script type="text/javascript">
document.writeln(darr[now.getDay()]);
</script>
</p><p>
<script type="text/javascript">
document.writeln([now.getDate()]
+ narr[now.getDate()]
+ " " + marr[now.getMonth()]
+ " " + now.getFullYear());
</script>
</p>

Notice that this requires two scripts rather than one. In fact, I couldn't be bothered coding for the superscript - that would have required 4 scripts! Still, at least it is now valid code.

I'm still not entirely sure of the logic behind this but it's worth bearing in mind if you do include any javascript in your pages.

francis
28th Nov 2005, 10:49 pm
Indeed, document.write isn't allowed in XHTML Strict (http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite), you have to use the DOM to add/remove elements. The DOM is a fantastic thing and, although tricky at first, is very worthwhile getting into. I can strongly recommend Jeremy Keith's DOM Scripting book for those who who have been put off JS and want to learn it. It's how starter books should be written.

For what you're wanting above, surely a server-side solution would be neater?

James
29th Nov 2005, 07:52 am
Problem with server side code for times/dates is it uses the server host country's time/date, whereas JavaScript gives the local time. Unless you want to write an incredibly complex script that sniffs the browser's country of origin and compares it to a time zone table and then adjusts accordingly! :o

I'm a little fed up with the strictness of some of these standards. Whatever happened to the free and easy web where anyone can publish? Fact is, millions of sites don't even attempt to comply with strict XHTML, so I'm sure browsers will recognise old code for decades to come.

Personally, I just check that my sites work OK if someone has JavaScript turned off. Beyond that, I'm not bothered about strict compliance at the moment.

David
29th Nov 2005, 09:16 am
Thanks for that Francis. I'd love to get going with DOM scripting but time does not permit that right now - I've already spent too much time on this thing. I'm just content that the page now validates - looks as though document.write is OK providing that it does not generate any markup.

James, you are right. The javascript date is much easier to handle than php when you just want the current time/date on the users computer.

You might feel that adding the current date to a web page is just unnecessary frippery and on the face of it, I would agree. The reason I'm using it here is that it has been a feature of the CADTutor site for some time and it's part of the personality, so I'm keeping it for continuity. Initially, I used it to simply provide a dynamic element in an otherwise static page and I still think it gives a "fresh" feeling to the site, even if nothing much is changing.

James, I also see where you're coming from with the compliance thing. As a pragmatist myself, I do agree that this all seems rather esoteric right now but I'm in a position where I have to be seen to be doing the right thing (practice what you preach and all that) so I am making the extra effort that requires.