By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,288 Members | 1,695 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,288 IT Pros & Developers. It's quick & easy.

Weird error...

P: n/a
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>

Jul 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
> 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/

Jul 20 '05 #2

P: n/a
On Thu, 10 Jul 2003 22:52:12 -0700, "Douglas Crockford"
<no****@laserlink.net> wrote:
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.


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.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #3

P: n/a
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.

Jul 20 '05 #4

P: n/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.


Which browser is that?

Jul 20 '05 #5

P: n/a
On Mon, 14 Jul 2003 17:09:05 -0700, "Douglas Crockford"
<no****@laserlink.net> wrote:
>W3C doesn't like strings containing '</'. Fortunately, browser makers aremuch >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.


Which browser is that?


XSmiles (XHTML only), Amaya, emacs-w3.

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #6

P: n/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.
Which browser is that?
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.

Jul 20 '05 #7

P: n/a
On Mon, 14 Jul 2003 17:32:18 -0700, "Douglas Crockford"
<no****@laserlink.net> wrote:
>> >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. >Which browser is that?

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.


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.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.