Weird error...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Francois

    Weird error...

    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.get Time()/1;
    rnd=today%len;
    document.writel n('<a href="'+eval("l nk"+rnd)+'"><IM G
    SRC="'+eval("gf x"+rnd)+'"alt=" '+eval("alt"+rn d)+'"><br>'+eva l("txt"+rnd)+ '</
    a>'); //HERE'S THE PROBLEM!

    </script>

  • Douglas Crockford

    #2
    Re: Weird error...

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

    document.writel n('<a href="'+eval("l nk"+rnd)+'"><IM G
    SRC="'+eval("gf x"+rnd)+'"alt=" '+eval("alt"+rn d)+'"><br>'+eva l("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.writel n('<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.



    Comment

    • Jim Ley

      #3
      Re: Weird error...

      On Thu, 10 Jul 2003 22:52:12 -0700, "Douglas Crockford"
      <nospam@laserli nk.net> wrote:
      [color=blue][color=green]
      >> 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".[/color]
      >
      > document.writel n('<a href="'+eval("l nk"+rnd)+'"><IM G
      > SRC="'+eval("gf x"+rnd)+'"alt=" '+eval("alt"+rn d)+'"><br>'+eva l("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.[/color]

      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.javas cript FAQ - http://jibbering.com/faq/

      Comment

      • Francois

        #4
        Re: Weird error...

        [color=blue]
        > More importantly, all 4 calls to eval are evil and unnecessary. Remove them at
        > once. You can replace them
        >[/color]
        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.
        [color=blue]
        > If you are using eval(), you are almost certainly doing something very wrong.
        >[/color]
        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.

        Comment

        • Douglas Crockford

          #5
          Re: Weird error...

          > >W3C doesn't like strings containing '</'. Fortunately, browser makers are
          much[color=blue][color=green]
          > >smarter than W3C, so that isn't a problem in practice.[/color][/color]
          [color=blue]
          > Unfortunately not, there's at least one browser which is correct in
          > its handling of the above, probably more.[/color]

          Which browser is that?

          Comment

          • Jim Ley

            #6
            Re: Weird error...

            On Mon, 14 Jul 2003 17:09:05 -0700, "Douglas Crockford"
            <nospam@laserli nk.net> wrote:
            [color=blue][color=green][color=darkred]
            >> >W3C doesn't like strings containing '</'. Fortunately, browser makers are[/color][/color]
            >much[color=green][color=darkred]
            >> >smarter than W3C, so that isn't a problem in practice.[/color][/color]
            >[color=green]
            >> Unfortunately not, there's at least one browser which is correct in
            >> its handling of the above, probably more.[/color]
            >
            >Which browser is that?[/color]

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

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

            Comment

            • Douglas Crockford

              #7
              Re: Weird error...

              > >> >W3C doesn't like strings containing '</'. Fortunately, browser makers are[color=blue][color=green][color=darkred]
              > >> >much smarter than W3C, so that isn't a problem in practice.[/color][/color][/color]
              [color=blue][color=green][color=darkred]
              > >> Unfortunately not, there's at least one browser which is correct in
              > >> its handling of the above, probably more.[/color][/color][/color]
              [color=blue][color=green]
              > >Which browser is that?[/color][/color]
              [color=blue]
              > XSmiles (XHTML only), Amaya, emacs-w3.[/color]

              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.

              Comment

              • Jim Ley

                #8
                Re: Weird error...

                On Mon, 14 Jul 2003 17:32:18 -0700, "Douglas Crockford"
                <nospam@laserli nk.net> wrote:
                [color=blue][color=green][color=darkred]
                >> >> >W3C doesn't like strings containing '</'. Fortunately, browser makers are
                >> >> >much smarter than W3C, so that isn't a problem in practice.[/color][/color]
                >[color=green][color=darkred]
                >> >> Unfortunately not, there's at least one browser which is correct in
                >> >> its handling of the above, probably more.[/color][/color]
                >[color=green][color=darkred]
                >> >Which browser is that?[/color][/color]
                >[color=green]
                >> XSmiles (XHTML only), Amaya, emacs-w3.[/color]
                >
                >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.[/color]

                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.javas cript FAQ - http://jibbering.com/faq/

                Comment

                Working...