Weird error...

F

Francois

Salut - Hello,

I'm trying to comply with W3C HTML 4.01 Strict standards BUT one JS code is
causing me troubles. The parser returns an error of "having a closure tag
</a> with no other tag open before".

This is weird because the Js is OK but now I need to change it to comply
with W3C standards AND accomplish the same simple thing. Can you help?

Merci!

--------- here's the code ----------

<script type="text/javascript">

gfx0="http://www.etc";
lnk0="http://www.etc";
alt0="some text";
txt0="some text";
gfx1="http://www.etc";
lnk1="http://www.etc";
alt1="some text";
txt1="some text";
gfx2="http://www.etc";
lnk2="http://www.etc";
alt2="some text";
txt2="some text";
gfx3="http://www.etc";
lnk3="http://www.etc";
alt3="some text";
txt3="some text";
len=4;

today=new Date();
today=today.getTime()/1;
rnd=today%len;
document.writeln('<a href="'+eval("lnk"+rnd)+'"><IMG
SRC="'+eval("gfx"+rnd)+'"alt="'+eval("alt"+rnd)+'"><br>'+eval("txt"+rnd)+'</
a>'); //HERE'S THE PROBLEM!

</script>
 
D

Douglas Crockford

I'm trying to comply with W3C HTML 4.01 Strict standards BUT one JS code is
causing me troubles. The parser returns an error of "having a closure tag
</a> with no other tag open before".

document.writeln('<a href="'+eval("lnk"+rnd)+'"><IMG
SRC="'+eval("gfx"+rnd)+'"alt="'+eval("alt"+rnd)+'"><br>'+eval("txt"+rnd)+
'</a>');

W3C doesn't like strings containing '</'. Fortunately, browser makers are much
smarter than W3C, so that isn't a problem in practice. You might want to change
'</a>' to '<\/a>'. The JavaScript compiler will ignore the '\'.

More importantly, all 4 calls to eval are evil and unnecessary. Remove them at
once. You can replace them

window["lnk" + rnd]

Much better would be to use proper data structures.

data = [
{gfx: "http://www.etc", lnk: "http://www.etc",
alt: "some text", txt: "some text"},
{gfx: "http://www.etc", lnk: "http://www.etc",
alt: "some text", txt: "some text"},
{gfx: "http://www.etc", lnk: "http://www.etc",
alt: "some text", txt: "some text"},
{gfx: "http://www.etc", lnk: "http://www.etc",
alt: "some text", txt: "some text"}];

var item = data[Math.floor(Math.random() * data.length)];

document.writeln('<a href="' + item.lnk +
'"><img src="' + item.gfx +
'"alt="' + item.alt +
'"><br>' + item.txt + '<\/a>')

Don't use similar variables when you really want an array. If you want a random
number, use the random() function. If you are using eval(), you are almost
certainly doing something very wrong.

http://www.crockford.com/
 
J

Jim Ley

document.writeln('<a href="'+eval("lnk"+rnd)+'"><IMG
SRC="'+eval("gfx"+rnd)+'"alt="'+eval("alt"+rnd)+'"><br>'+eval("txt"+rnd)+
'</a>');

W3C doesn't like strings containing '</'. Fortunately, browser makers are much
smarter than W3C, so that isn't a problem in practice.

Unfortunately not, there's at least one browser which is correct in
its handling of the above, probably more. Always safest to listen to
the validator (except when it's wrong of course, then it's best to
listen to any 2 validators out of 3 and pester for a bugfix...)

Jim.
 
F

Francois

More importantly, all 4 calls to eval are evil and unnecessary. Remove them at
once. You can replace them
Thanks, your suggestion did what I wanted. Now it is only important for me
to know if this script is compatible with 4.x browsers.
If you are using eval(), you are almost certainly doing something very wrong.
I'm using random() function.

Finally, I would like to know how can I change the script to have random
phrases appearing on my pages with no images but with links.
 
D

Douglas Crockford

W3C doesn't like strings containing '</'. Fortunately, browser makers are
much
Unfortunately not, there's at least one browser which is correct in
its handling of the above, probably more.

Which browser is that?
 
D

Douglas Crockford

W3C doesn't like strings containing '</'. Fortunately, browser makers are
XSmiles (XHTML only), Amaya, emacs-w3.

I have to admit, you had me going there for a minute.

Are you suggesting that people write to Amaya? It's a waste of time.
 
J

Jim Ley

I have to admit, you had me going there for a minute.

Are you suggesting that people write to Amaya? It's a waste of time.

but X-Smiles surely isn't? Especially as it's a good indicator to the
future direction of viewers.

Also there's little point in ignoring validation, what does it get
you?

Incidently, I gave JSON a plug at SVGOpen today
(http://jibbering.com/talk/ )

Jim.
 

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

No members online now.

Forum statistics

Threads
474,076
Messages
2,570,565
Members
47,200
Latest member
Vanessa98N

Latest Threads

Top