slashes in HTML tags in JavaScript

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

    #1

    slashes in HTML tags in JavaScript

    I have noticed with various codes that the slashes are not consistent. Is there a reason for this.

    For example

    </td>
    </\td>
    <\/td>
    <td />

    what is the difference with these tags?


    also, tags that do not normally have a slash in them, such as the <br> command I have seen with a
    slash at the end, such as <br />


    Pardon my ignorance, but is there a reason for the extra slashes?


  • Paul Wellner Bou

    #2
    Re: slashes in HTML tags in JavaScript

    Treetop wrote:

    Well, that's HTML and not JavaScript.
    But anyway.

    </td> <-- closing Tag of <td>
    </\td> <-- doesn't exist in HTML/XHTML
    <\/td> <-- doesn't exist also
    <td /> <-- represents an empty element, used in XML and XHTML
    to represent an element like <td></td> with no content.
    [color=blue]
    > also, tags that do not normally have a slash in them,
    > such as the <br> command I have seen with a
    > slash at the end, such as <br />[/color]

    Like mentioned yet, <br> is an empty element with no content.
    Never. In good old HTML you can write just <br>. In XHTML and
    XML (well, in XML you can use any element names, so theoretically
    you could give content to a <br> element) you have to write
    <br />

    The backslash isn't used in the Markup Languages like HTML,
    XHTML or XML or similars, but he is used in JavaScript (and other
    languages, so in PHP, too, f.e.) to escape certain characters
    like quotations, line breaks, tabs, etc.

    line break: \n
    tab: \t
    quotations: \"

    You can escape the slash, too. I think you don't have to,
    but you can. So this:

    alert('<\/td>');

    will give you an alert showing just:

    </td>

    and that's the normal closing tag for a <td> before.

    This sample in JavaScript: </\td> would return you a string
    like this:

    </ d>

    (with a tab between / and d, becaus \t is used for the
    tab character.

    I hope I helped you ;-)

    Saludo
    Paul.

    Comment

    • Jim Ley

      #3
      Re: slashes in HTML tags in JavaScript

      On Tue, 07 Oct 2003 15:33:46 GMT, "Treetop" <treetop@netfro nt.net>
      wrote:

      [You got a good response to the others]
      [color=blue]
      >also, tags that do not normally have a slash in them, such as the <br>
      > command I have seen with a slash at the end, such as <br />
      >
      >Pardon my ignorance, but is there a reason for the extra slashes?[/color]

      Hype Monkeys - some people heard about XHTML and stuck them in,
      probably best avoided and sticking with HTML 4.01 until we see some
      real implementations .

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

      Comment

      • Treetop

        #4
        Re: slashes in HTML tags in JavaScript

        Thanks for the info. I would rather code in HTML 3 or 4. I just wanted to make sure that I didn't
        have to do anything special with my HTML as I start to learn JavaScript, such as the <br />.


        "Paul Wellner Bou" <paul.wellner@u nited-scripts.com> wrote in message
        news:oprwokerta q6cjlk@news.ind ividual.net...[color=blue]
        > Treetop wrote:
        >
        > Well, that's HTML and not JavaScript.
        > But anyway.
        >
        > </td> <-- closing Tag of <td>
        > </\td> <-- doesn't exist in HTML/XHTML
        > <\/td> <-- doesn't exist also
        > <td /> <-- represents an empty element, used in XML and XHTML
        > to represent an element like <td></td> with no content.
        >[color=green]
        > > also, tags that do not normally have a slash in them,
        > > such as the <br> command I have seen with a
        > > slash at the end, such as <br />[/color]
        >
        > Like mentioned yet, <br> is an empty element with no content.
        > Never. In good old HTML you can write just <br>. In XHTML and
        > XML (well, in XML you can use any element names, so theoretically
        > you could give content to a <br> element) you have to write
        > <br />
        >
        > The backslash isn't used in the Markup Languages like HTML,
        > XHTML or XML or similars, but he is used in JavaScript (and other
        > languages, so in PHP, too, f.e.) to escape certain characters
        > like quotations, line breaks, tabs, etc.
        >
        > line break: \n
        > tab: \t
        > quotations: \"
        >
        > You can escape the slash, too. I think you don't have to,
        > but you can. So this:
        >
        > alert('<\/td>');
        >
        > will give you an alert showing just:
        >
        > </td>
        >
        > and that's the normal closing tag for a <td> before.
        >
        > This sample in JavaScript: </\td> would return you a string
        > like this:
        >
        > </ d>
        >
        > (with a tab between / and d, becaus \t is used for the
        > tab character.
        >
        > I hope I helped you ;-)
        >
        > Saludo
        > Paul.[/color]


        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: slashes in HTML tags in JavaScript

          Paul Wellner Bou <paul.wellner@u nited-scripts.com> writes:
          [color=blue]
          > You can escape the slash, too. I think you don't have to,
          > but you can. So this:
          >
          > alert('<\/td>');
          >
          > will give you an alert showing just:
          >
          > </td>[/color]

          According to the HTML specification, you do have to insert the
          backslash.

          The contents of a script tag is not interpreted as HTML, so if you
          have "<td>" in the code, it is just a Javascript string. However,
          the terminating </script> must be interpreted as HTML.

          The specification actually says that the script tag is terminated
          at the first occurence of "</" (the end-tag-start-marker). To avoid
          premature termination, the easiest way, and recommended in the
          HTML specification, is to escape the slash.

          In practice, most current browsers are forgiving, and won't stop at
          the first "</", but will only stop at a script end tag, "</script>".

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Dr John Stockton

            #6
            Re: slashes in HTML tags in JavaScript

            JRS: In article <79b76459f16a6a 2ca9a870d5a6b54 d34@news.terane ws.com>,
            seen in news:comp.lang. javascript, Treetop <treetop@netfro nt.net> posted
            at Tue, 7 Oct 2003 15:33:46 :-[color=blue]
            >
            ><\/td>[/color]

            In a javascript string, </td>, etc., annoy at least one HTML checker
            (W3/sourceforge's TIDY), whereas <\/td>, etc., do not, and work just as
            well.

            All pages should be HTML-checked, since it is a good way of finding
            typos in the HTML language, and a page with a typo cannot be relied on
            to render as you would wish in some or all browsers. Neither can other
            pages, but having no typos helps.

            One does not want even harmless errors reported, since if that is
            allowed there is a risk of missing potentially harmful ones.

            For those in a recent thread, "Q re white paper on Escaping characters":
            that may be an example of backslash followed by a non-listed character,
            which is supposed to be deprecated.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

            Comment

            Working...