Document.Write with HTML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis M. Marks

    Document.Write with HTML

    I generate a SELECT list dynamically by taking items from a table. I
    have DOCUMENT.WRITE statements that write a combination of literals and
    variables including < and > characters. The HTML seems to work all
    right but validators show errors of having a closing > without an
    opening <. Even though these are in DOCUMENT.WRITE statements within
    quotes they are still flagged as errors.

    Is there any way around this. I have tried HTML entities but they cause
    problems. They seem to be substituted at the wrong time causing errors.

    The page is http://www.dcs-chico.com/~denmarks/amtrak.html

    BTW: I've been told that the ID statements are incorrect and I should
    use CLASS. ID seems to work. If I should use CLASS could somebody show
    me exactly what to change.

    I was a mainframe programmer all of my life and javascript is new to
    me. I don't care at this time if there is a better way to do the page.
    It is for fun and I just want it to be correct.

    --
    Dennis M. Marks


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
  • Joshie Surber

    #2
    Re: Document.Write with HTML

    you need to enclose your <script> tags in a CDATA element.


    Comment

    • Graham J

      #3
      Re: Document.Write with HTML

      > I generate a SELECT list dynamically by taking items from a table. I[color=blue]
      > have DOCUMENT.WRITE statements that write a combination of literals and
      > variables including < and > characters. The HTML seems to work all
      > right but validators show errors of having a closing > without an
      > opening <. Even though these are in DOCUMENT.WRITE statements within
      > quotes they are still flagged as errors.[/color]

      Think of it from an HTML validator's point of view. You have opened a
      <script> element which is stuffed full of non-HTML that it cannot interpret
      as anything but a stream of characters. It is searching out the closing
      HTML tag. When it comes across </ in the script it recognises a closing tag
      and then is baffled to find that it isn't a </script> and so complains.
      What you do is to escape the / with a backslash so you get e.g.
      "....<\/option>..." in the document.write statements. This keeps the HTML
      validators happy and the JavaScript interpreter is happy too.
      [color=blue]
      > BTW: I've been told that the ID statements are incorrect and I should
      > use CLASS. ID seems to work. If I should use CLASS could somebody show
      > me exactly what to change.[/color]

      If you change e.g. id="red" to class="red" and in your style section change
      #red to .red you should be ok. The thing is that an id is supposed to be
      unique within the document. It identifies a particular instance.

      Note that you really should get into the habit of delimiting the tags with
      quotes i.e. id="red" not id=red (in the script you might need to escape the
      double quotes with a backslash if you use double quotes for the string). If
      they weren't in scripts the validators would probably pick you up on it.
      [color=blue]
      > I was a mainframe programmer all of my life and javascript is new to
      > me. I don't care at this time if there is a better way to do the page.
      > It is for fun and I just want it to be correct.[/color]

      Yes you have to start somewhere though it is better to start with good
      habits if you can. For example if you decide to change the colour scheme
      you might suddenly realise that having classes called "red", "green" and
      "blue" no longer seem quite such a good idea!


      Comment

      • Stephen Poley

        #4
        Re: Document.Write with HTML

        On Mon, 1 Dec 2003 09:14:21 -0000, "Graham J" <me@privacy.net > wrote:
        [color=blue]
        >Note that you really should get into the habit of delimiting the tags with
        >quotes i.e. id="red" not id=red (in the script you might need to escape the
        >double quotes with a backslash if you use double quotes for the string). If
        >they weren't in scripts the validators would probably pick you up on it.[/color]

        You mean attribute values rather than tags. (Tags are delimited by < and[color=blue]
        >). The delimiting quotes are not required in all circumstances, and I[/color]
        usually don't bother if the attribute value consists only of letters.
        The validators won't complain, because that is valid SGML.

        Some people do recommend always using the quotes, even when they are not
        required, on the grounds that you might change something and forget to
        add the quotes. But that's a judgement call.

        --
        Stephen Poley

        Comment

        • Graham J

          #5
          Re: Document.Write with HTML

          > You mean attribute values rather than tags.

          Oopsy! I'd spotted that having just talked about replacing id with class
          I'd then used id in the example but missed that I'd written tags by mistake!
          [color=blue]
          > The delimiting quotes are not required in all circumstances, and I
          > usually don't bother if the attribute value consists only of letters.
          > The validators won't complain, because that is valid SGML.[/color]

          Yes, what I was trying (and failing) to say was that not having quotes when
          you are supposed to have them is another thing some browsers might let you
          get away with only for validators to pull you up.
          [color=blue]
          > Some people do recommend always using the quotes, even when they are not
          > required, on the grounds that you might change something and forget to
          > add the quotes. But that's a judgement call.[/color]

          IMHO better to make a habit of using the quotes first and then learning
          where you can drop them but as you say like several other things web page
          related it is down to personal taste. As I say I was really responding in
          the context of a situation where browsers allow things to happen but
          validators don't and showing another area where it could happen.

          Comment

          Working...