Everything works but IE

A

A Browne

I have this piece of code on a page footer;

<script type="text/javascript">
//<![CDATA[
<!--
if (Date.parse(document.lastModified) > 0) {
var last = new Date(document.lastModified);
document.write(last.toLocaleString());
}
-->
//]]>
</script>

It displays the last modified date perfectly in every browser I test it
on - except IE, where it does nothing.
(To specify - it works on Firefox on Windows and on Firefox, Safari and
Camino on Mac. It fails on IE on Windows. They're all I have.)

Can someone please tell me why?

TIA
Alex
 
V

Vincent van Beveren

Can someone please tell me why?
Apprently IE has trouble with the CDATA elements. Unless its vitally
important I would just remove it. The problem is that IE expects <!--
immidiatly after <script>, if not it'll try to interpret it as a
statement. So:
<!--
//<![CDATA[
if (Date.parse(document.lastModified) > 0) {
var last = new Date(document.lastModified);
document.write(last.toLocaleString());
}
//]]>
-->

would work, but I doubt if it is valid XHTML.

Vincent
 
R

Richard Cornford

Vincent van Beveren wrote:
<!--
//<![CDATA[
//]]>
-->

would work, but I doubt if it is valid XHTML.

Why not? Comments are valid in XHTML. Of course the fact that XML
parsers are allowed to remove comments means that the entire script may
never be seen if the document is interpreted as XHTML. Fortunately the
odds are that this document is designed to be interpreted as HTML only,
and the CDATA markers are just another mystical incantation.

Richard.
 
R

Randy Webb

Vincent van Beveren said the following on 7/26/2006 7:20 AM:
Apprently IE has trouble with the CDATA elements.

No, it has problems with syntax errors.
Unless its vitally important I would just remove it.

Yes, indeed.
The problem is that IE expects <!-- immidiatly after <script>,

No, it doesn't expect <!-- and it never has. If it sees it, it ignores
it. If it doesn't, then it moves on.
if not it'll try to interpret it as a statement.

Wrong. The first statement was //<![CDATA[ which it properly interprets
as a comment line.

So:
<!--
//<![CDATA[
if (Date.parse(document.lastModified) > 0) {
var last = new Date(document.lastModified);
document.write(last.toLocaleString());
}
//]]>

Look at the original code and the line before your last was --> which is
a syntax error and IE balks on *that* error, not the CDATA.
would work, but I doubt if it is valid XHTML.

Yes, because you corrected the syntax error. And with regards to IE, it
doesn't matter if it is valid XHTML or not as IE doesn't have a clue
what XHTML is.

<script type="text/javascript">
//<![CDATA[
if (Date.parse(document.lastModified) > 0) {
var last = new Date(document.lastModified);
document.write(last.toLocaleString());
}
//]]>
</script>


No opening <!-- and it does what's wanted in IE.
 
A

A Browne

Vincent van Beveren said the following on 26/07/2006 12:20:
Apprently IE has trouble with the CDATA elements. Unless its vitally
important I would just remove it. The problem is that IE expects <!--
immidiatly after <script>, if not it'll try to interpret it as a
statement.

Vincent,

Thanks. I tried switching the <!-- to before the CDATA (and similarly
the end tags) and it works in IE, but incorrectly in the others.

Odd thing is that there's other script code using the CDATA element that
does seem to work across the board.

Oh well.
Alex
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Wed, 26 Jul 2006 10:57:17 remote, seen in A
Browne said:
if (Date.parse(document.lastModified) > 0) {
var last = new Date(document.lastModified);
document.write(last.toLocaleString());
}


That code does not write in IE4 - but maybe that's correct. Executing
in local master of <URL:http://www.merlyn.demon.co.uk/js-quick.htm>,

document.lastModified 07/24/06 16:48:28 FFF & Y2k, otherwise OK

Date.parse(document.lastModified) -2002003892000

06 is interpreted as 1906, before Epoch, hence negative answer; hence
rest ignored. The code does what it should do for me.

It's silly to throw away the Date.parse result, since it is an ideal
argument for new Date - why do string-to-number conversion twice?

OTOH you could drop the Date.parse and use new Date() there ;

if ((D = new Date(document.lastModified))>1e12)
document.write(D.toLocaleString());

which works for me apart from the Y2k error and the misfortune that
toLocaleString() incorrectly gives me an FFF date.

The 1e12 corresponds to 2001-09-09 01:46:40 UTC; it seems safe to assume
that any page you code in future will be coded after that.


Check, as I did, your code piece by piece, starting with the
lastModified string itself.

Read the newsgroup FAQ. Then re-read 4.30 & follow link.
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top