Debug Request

D

Dr J R Stockton

The page below works in Firefox & Safari, but not in IE & Opera. How
should it be put right? The idea is that the TDs will be loaded with a
fully-specified date/time, preferably as UTC, but those users with
JavaScript running will see the date/times as Local Civil Time. Tests
in IE suggest that PT is indeed set to an Object, but that the loop is
never entered - T is false on the first entry.

TIDY shows three like
line 29 column 5 - Warning: <td> proprietary attribute "name"
which explains what is wrong.

Best would be something that can mark for action any element suitable
for containing a datetime (TH, TD, SPAN, ...?) but not to search the
body for any datetime.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>xxx</title>
</head>

<body onLoad="BB()">

<script type="text/javascript">

function Fmt(D) {
return D.getFullYear() + '-' + LZ(D.getMonth()+1) + '-' +
LZ(D.getDate()) + " " + LZ(D.getHours()) + ':' +
LZ(D.getMinutes()) + " LCT" }

function LZ(n) { return (n<10?"0":"") + n }

function BB() { var J=0, PT, T, D
PT = document.getElementsByName("AA")
while (T=PT[J++]) {
D = new Date(T.innerHTML.replace(/-/g, "/"))
T.innerHTML = Fmt(D) }
}

</script>

<table border=1 summary=OK>
<tr><td name=AA>2008-06-01 07:07:07 UTC</td><td>more</td></tr>
<tr><td name=AA>2008-06-02 07:07:07 EDT</td><td>"</td></tr>
<tr><td name=AA>2008-06-03 07:07:07 PDT</td><td>"</td></tr>
</table>

</body>
</html>
 
D

Dr J R Stockton

...
The idea is that the TDs will be loaded with a
fully-specified date/time, preferably as UTC, but those users with
JavaScript running will see the date/times as Local Civil Time.
...

Using Web view of Example 15-4 from JavaScript: The Definitive Guide,
Fifth Edition, problem seems solved. Now using class=QQ to mark the
targets, and the following onLoad code. Comment?

function CC() { var J=0, PT, T, D
PT = document.getElementsByTagName("*") // ; alert(PT.length)
while (T=PT[J++]) if (T.className=="QQ") {
D = new Date(T.innerHTML.replace(/-/g, "/"))
T.innerHTML = Fmt(D) }
}

I do need to adjust it to deal better with bad input; no problem
AFAICS.
 
D

dhtml

Conrad said:
On 2008-10-21 18:56, Dr J R Stockton wrote:
As a side note, I also found your code very hard to read; you will know
what variables like PT, J, T, and functions like BB and LZ are supposed
to represent, but I didn't, and if your script had been any longer than
this, I wouldn't have bothered to look at it.

Undeclared variables end up becoming global properties (or sometimes
errors in IE). Declare them using var.

var dateContainer = document.getElementById('myTbody'),
dateCells = dateContainer.getElementsByTagName('td');

Garrett
 
D

Dr J R Stockton

It does work in Opera (opera_9.61.2456.gcc4.qt3_i386).

That was 9.27. I keep 9.27 on this machine until I have *memorised*
the replacement fot Ctrl-Alt-V.

The usual solution is to assign CSS classes and use something like
getElementsByClassName, which will have to be retrofitted for some
browsers. Microformats in HTML work the same way; they (mostly) use
class names to add semantic information to generic markup:http://en.wikipedia.org/wiki/Microformat

As you can see from my second post in this thread, I discovered that.
If "Microformat" becomes common in CLJ or FAQ, it's a glossary
candidate.


There remains one strangeness. Whilst the code is intended to be
called by body onLoad, therefore only once, the demo version now at
the foot of js-tests9.htm has a button. In IE, FF, Op, Sf, Ch, the
button works when first pressed. In all bar Opera, it works (though
not usefully) on a second press. In Opera (9.27, 9.61), the button
sort of seems to be stuck down, although other buttons on the page
still work. So it does not matter, but there's a bug or an amazing
feature somewhere.

As a side note, I also found your code very hard to read; you will know
what variables like PT, J, T, and functions like BB and LZ are supposed
to represent, but I didn't, and if your script had been any longer than
this, I wouldn't have bothered to look at it.

I like short variables when their lexical scope is short, or, like LZ,
I use them a lot; and I like them in new code. It eliminates one
class of bug : e.g. all wrong ways of spelling J are obvious at a
glance. They have neen lengthened in js-date9.htm#LCT.

IMHO, LZ should be in the FAQ; either in a Date context, or as an
example of how lexically-repetitive actions should be treated.


I don't see how dhtml's remark on undeclared variables might apply
here.
 
D

Dr J R Stockton

Dr J R Stockton wrote:

[js-date9.htm#LCT]
There remains one strangeness. Whilst the code is intended to be
called by body onLoad, therefore only once, the demo version now at
the foot of js-tests9.htm has a button. In IE, FF, Op, Sf, Ch, the
button works when first pressed. In all bar Opera, it works (though
not usefully) on a second press. In Opera (9.27, 9.61), the button
sort of seems to be stuck down, although other buttons on the page
still work. So it does not matter, but there's a bug or an amazing
feature somewhere.

Explained. Opera ignores alphabetic zone offset indications, apart
from GMT & UTC, AFAICS. Therefote it ignores LCT, which stands
for Local Civil Time but is not expected to be recognised by a
browser.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top