Does <script src=foo> require an end tag </script>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rhythmace@gmail.com

    Does <script src=foo> require an end tag </script>

    W3C HTML validator passes this:

    ....
    <script type="text/javascript" src="foo.js">

    <script type="text/javascript">
    ....script in here...
    </script>
    ....

    But as far as I can tell, the first script tag requires a </script>
    close tag. Is the close tag required for valid markup? I know browsers
    accept this, but that's not the question. Is it valid?

  • Eric B. Bednarz

    #2
    Re: Does &lt;script src=foo&gt; require an end tag &lt;/script&gt;

    rhythmace@gmail .com writes:
    [color=blue]
    > W3C HTML validator passes this:[/color]

    Here's your SCRIPT element type start tag:
    [color=blue]
    > <script type="text/javascript" src="foo.js">[/color]

    Here's some arbitrary CDATA content:
    [color=blue]
    > <script type="text/javascript">
    > ...script in here...[/color]

    Here's your SCRIPT element type end tag:
    [color=blue]
    > </script>[/color]

    Next time, enable 'view parse tree' (or whatever it is called, I can't
    be bothered to have a look).
    [color=blue]
    > But as far as I can tell, the first script tag requires a </script>
    > close tag.[/color]

    Yes. Well, end tag.
    [color=blue]
    > Is the close tag required for valid markup?[/color]

    Yes.
    [color=blue]
    > I know browsers
    > accept this,[/color]

    Ah. I don't, I have too many of them to make generalised statements
    about their treatment of generalized markup.
    [color=blue]
    > but that's not the question. Is it valid?[/color]

    Yes.


    (who cares?)


    --
    ||| hexadecimal EBB
    o-o decimal 3771
    --oOo--( )--oOo-- octal 7273
    205 goodbye binary 111010111011

    Comment

    • Toby Inkster

      #3
      Re: Does &lt;script src=foo&gt; require an end tag &lt;/script&gt;

      rhythmace wrote:
      [color=blue]
      > <script type="text/javascript" src="foo.js">
      >
      > <script type="text/javascript">
      > ...script in here...
      > </script>
      > ...
      >
      > But as far as I can tell, the first script tag requires a </script>
      > close tag. Is the close tag required for valid markup?[/color]

      Yes -- the SCRIPT element requires an end tag. But you have an end tag
      there.

      The real question, is "how many SCRIPT elements do you have there?" The
      answer is one.

      The second "<script... " bit will be treated as Javascript data within the
      first SCRIPT element, and should cause a Javascript error (but not a
      validation error).

      --
      Toby A Inkster BSc (Hons) ARCS
      Contact Me ~ http://tobyinkster.co.uk/contact

      Comment

      • VK

        #4
        Re: Does &lt;script src=foo&gt; require an end tag &lt;/script&gt;


        rhythmace@gmail .com wrote:[color=blue]
        > <script type="text/javascript" src="foo.js">
        >
        > <script type="text/javascript">
        > ...script in here...
        > </script>[/color]

        That was already explained why such script declaration will not work.
        I'd just like to alert you about another common mistake of this kind:

        <script type="text/javascript" src="foo.js">
        // code here
        </script>

        // code here block will be ignored and only foo.js code will be
        executed (unless it's Netscape 2.x w/o src attribute support)

        So the rule is: external file requires separate <script> block with
        closing tag.

        The only working option:

        <script type="text/javascript" src="foo.js"></script>
        <script type="text/javascript">
        // code here
        </script>

        Comment

        • rhythmace@gmail.com

          #5
          Re: Does &lt;script src=foo&gt; require an end tag &lt;/script&gt;

          Excellent! Thanks so much guys. Actually the browsers appear to be
          smart about this and afaict they do interpret 2 script tags, not 1, so
          they might have some heuristic. For example, if it is not possible to
          nest script tags then maybe they just autoclose the first one???

          Comment

          • VK

            #6
            Re: Does &lt;script src=foo&gt; require an end tag &lt;/script&gt;


            rhythmace@gmail .com wrote:[color=blue]
            > Actually the browsers appear to be
            > smart about this and afaict they do interpret 2 script tags, not 1, so
            > they might have some heuristic.[/color]

            No they don't: it is your allusion. Probably you happened to have a
            variable/function you're checking against both in external file and in
            inline code.

            Any way: your syntax doesn't work (and cannot work), so please stop
            confusing potentional readers ;-) :-|

            A code to dismiss your allusion:

            // THIS SYNTAX IS INTENTIONALLY WRONG
            // FOR DEMONSTRATION PURPOSES

            <html>
            <head>
            <title>Test</title>
            <meta http-equiv="Content-Type"
            content="text/html; charset=iso-8859-1">
            <script type="text/javascript" src="foo.js" />
            <script type="text/javascript">
            var v = 1;
            </script>
            </head>

            <body onLoad="alert(t ypeof v)">

            </body>
            </html>

            // FF 1.5.0.1 : undefined
            // IE 6.0 : undefined
            // Opera 8.5 : undefined
            And for a sake of it:
            // NN 4.5 : undefined

            Comment

            • Toby Inkster

              #7
              Re: Does &lt;script src=foo&gt; require an end tag &lt;/script&gt;

              VK wrote:
              [color=blue]
              > // THIS SYNTAX IS INTENTIONALLY WRONG
              > // FOR DEMONSTRATION PURPOSES
              >[/color]
              [...][color=blue]
              > <script type="text/javascript" src="foo.js" />
              > <script type="text/javascript">
              > var v = 1;
              > </script>[/color]

              That should actually be fine, assuming XHTML.

              --
              Toby A Inkster BSc (Hons) ARCS
              Contact Me ~ http://tobyinkster.co.uk/contact

              Comment

              • VK

                #8
                Re: Does &lt;script src=foo&gt; require an end tag &lt;/script&gt;


                Toby Inkster wrote:[color=blue]
                > VK wrote:
                >[color=green]
                > > // THIS SYNTAX IS INTENTIONALLY WRONG
                > > // FOR DEMONSTRATION PURPOSES
                > >[/color]
                > [...][color=green]
                > > <script type="text/javascript" src="foo.js" />
                > > <script type="text/javascript">
                > > var v = 1;
                > > </script>[/color]
                >
                > That should actually be fine, assuming XHTML.[/color]

                Of course it is *not*. The parsing rules get stricter, but the basics
                are the same. <script> tag requires closing tag. After meeting <script
                type="text/javascript" src="foo.js" /> UA will ignore everything until
                it meets the closing tag </script>. This way the internal block will be
                ignored.

                You seem to have a strange idea about the /> syntax. It is not (and
                cannot be) used to override (X)HTML standards - say to transform a
                paired tag into a single one against specifications. Why wouldn't you
                try to use then <div />Content 1 <div />Content 2

                Comment

                • Michael Winter

                  #9
                  Re: Does &lt;script src=foo&gt; require an end tag &lt;/script&gt;

                  On 12/04/2006 21:58, VK wrote:
                  [color=blue]
                  > Toby Inkster wrote:
                  >[color=green]
                  >> VK wrote:[/color][/color]

                  [snip]
                  [color=blue][color=green][color=darkred]
                  >>> <script type="text/javascript" src="foo.js" />[/color][/color][/color]

                  [snip]
                  [color=blue][color=green]
                  >> That should actually be fine, assuming XHTML.[/color]
                  >
                  > Of course it is *not*.[/color]

                  Yes, it is. Toby said XHTML, not HTML.

                  [snip]

                  Mike

                  --
                  Michael Winter
                  Prefix subject with [News] before replying by e-mail.

                  Comment

                  • VK

                    #10
                    Re: Does &lt;script src=foo&gt; require an end tag &lt;/script&gt;

                    > <script type="text/javascript" src="foo.js" />[color=blue]
                    > <script type="text/javascript">
                    > var v = 1;
                    > </script>[/color]


                    Michael Winter wrote:[color=blue]
                    > Yes, it is <fine>. Toby said XHTML, not HTML.[/color]

                    What do you mean by "fine"? That it will pass XHTML validation on W3C
                    validator? Yes it currently will because there is obviously a bug in
                    the current version. It allows you to use the /> syntacs against the
                    declared DTD - thus in paired tags. After the validator will learn to
                    pay more attention to the declared DTD's, this issue will be solved.

                    If by "fine" you mean "will produce the intended result" - then of
                    course and once again *no*. From two declared blocks above anly the
                    external file foo.js will be loaded.

                    Comment

                    • Michael Winter

                      #11
                      Re: Does &lt;script src=foo&gt; require an end tag &lt;/script&gt;

                      On 12/04/2006 22:52, VK wrote:

                      [snip]
                      [color=blue]
                      > Michael Winter wrote:
                      >[color=green]
                      >> Yes, it [an empty-element tag] is fine. Toby said XHTML, not HTML.[/color]
                      >
                      > What do you mean by "fine"?[/color]

                      That it, when read by an XML processor, will add a script element to the
                      document tree. What else do you think I could have meant?

                      [snip]

                      Mike

                      --
                      Michael Winter
                      Prefix subject with [News] before replying by e-mail.

                      Comment

                      • VK

                        #12
                        Re: Does &lt;script src=foo&gt; require an end tag &lt;/script&gt;


                        Michael Winter wrote:[color=blue][color=green]
                        > > What do you mean by "fine"?[/color]
                        >
                        > That it, when read by an XML processor, will add a script element to the
                        > document tree. What else do you think I could have meant?[/color]

                        By XHTML processor, not XML processor (there is a difference). Yes it
                        may slip through if a validator (or a parser) is buggy and doesn't use
                        declared DTD in parsing.

                        But the whole thread was about this block:

                        <script type="text/javascript" src="foo.js">
                        <script type="text/javascript">
                        ....script in here...
                        </script>

                        or this:
                        <script type="text/javascript" src="foo.js" />
                        <script type="text/javascript">
                        ....script in here...
                        </script>

                        As neither one will work by definition (only one script element with
                        external file will be created) I would be highly hesitating to call it
                        "fine" ;-)

                        The first one is: "A syntax allowed by HTML parsing rules but
                        practically useless, misleading and error prone".

                        The second one is: "A syntax allowed by some XHTML validators but
                        practically useless, misleading and error prone".

                        To be short I would use only the constant part of both descriptions: "A
                        practically useless, misleading and error prone syntax". ;-)

                        Comment

                        • Steve Pugh

                          #13
                          Re: Does &lt;script src=foo&gt; require an end tag &lt;/script&gt;

                          "VK" <schools_ring@y ahoo.com> wrote:
                          [color=blue][color=green]
                          >> <script type="text/javascript" src="foo.js" />
                          >> <script type="text/javascript">
                          >> var v = 1;
                          >> </script>[/color]
                          >
                          >Michael Winter wrote:[color=green]
                          >> Yes, it is <fine>. Toby said XHTML, not HTML.[/color]
                          >
                          >What do you mean by "fine"? That it will pass XHTML validation on W3C
                          >validator? Yes it currently will because there is obviously a bug in
                          >the current version. It allows you to use the /> syntacs against the
                          >declared DTD - thus in paired tags. After the validator will learn to
                          >pay more attention to the declared DTD's, this issue will be solved.[/color]

                          Care to state where in any XHTML DTD this requirement is?

                          There's a suggestion in the text of the XHTML 1.0 spec (not the DTD)
                          that /> only be used for elements that are defined as empty in HTML
                          4.01, for reasons of pseudo-compatability with (buggy) HTML parsing
                          engines.

                          But that's as far as it goes. The Validator only reads the DTD and not
                          the spec (that being what a validator does) there is no bug in the
                          validator.
                          [color=blue]
                          >If by "fine" you mean "will produce the intended result" - then of
                          >course and once again *no*. From two declared blocks above anly the
                          >external file foo.js will be loaded.[/color]

                          "Fine" as in perfectly good XHTML for creating two script elements?
                          The fact that browsers get it wrong is no big surprise.

                          Steve
                          --
                          "My theories appal you, my heresies outrage you,
                          I never answer letters and you don't like my tie." - The Doctor

                          Steve Pugh <steve@pugh.net > <http://steve.pugh.net/>

                          Comment

                          • VK

                            #14
                            Re: Does &lt;script src=foo&gt; require an end tag &lt;/script&gt;

                            Steve Pugh wrote:[color=blue]
                            > "Fine" as in perfectly good XHTML for creating two script elements?
                            > The fact that browsers get it wrong is no big surprise.[/color]

                            "No big deal that this code doesn't work on any of existing/ever
                            existed browsers and there are doubts that it ever will in any near
                            future. It is still fine - because it is theoretically correct."

                            Sometimes ciwas and siwah just nock me out: I feel like I got crazy -
                            or everyone around me did. :-)

                            It might help to accomodate if ciwah would be piwah:
                            philosophy.info systems.www.abstractions.html :-)

                            But as I see "authoring" instead of "abstractio ns" then could we be a
                            tiiiny bit more reality connected?

                            Comment

                            • Nick Kew

                              #15
                              Re: Does &lt;script src=foo&gt; require an end tag &lt;/script&gt;

                              Steve Pugh wrote:
                              [color=blue]
                              > Care to state where in any XHTML DTD this requirement is?
                              >
                              > There's a suggestion in the text of the XHTML 1.0 spec (not the DTD)
                              > that /> only be used for elements that are defined as empty in HTML
                              > 4.01, for reasons of pseudo-compatability with (buggy) HTML parsing
                              > engines.[/color]

                              Wrong.

                              Nothing to do with buggy HTML parsing. Everything to do with
                              taking advantage of error-tolerance in HTML parsers.
                              [color=blue]
                              > But that's as far as it goes. The Validator only reads the DTD and not
                              > the spec (that being what a validator does) there is no bug in the
                              > validator.[/color]

                              Indeedie. That's why validation is only one part of checking.
                              [color=blue][color=green]
                              >>If by "fine" you mean "will produce the intended result" - then of
                              >>course and once again *no*. From two declared blocks above anly the
                              >>external file foo.js will be loaded.[/color]
                              >
                              >
                              > "Fine" as in perfectly good XHTML for creating two script elements?
                              > The fact that browsers get it wrong is no big surprise.[/color]

                              What browsers get that wrong?

                              Hint: if you serve documents as "text/html", browsers rightly apply
                              HTML rules, not XML rules. So if you violate Appendix C - as in
                              the example given - browsers are right to see the first <script
                              as unclosed. If you want browsers to apply XML rules, serve
                              them XML MIME types.

                              --
                              not me guv

                              Comment

                              Working...