inline document.write(html) within <head> tag safe?

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

    inline document.write(html) within <head> tag safe?

    Is writing a document *during* page load safe
    and supported for all 4th and 5th generation browsers?
    If not, can you show me a specific example/browser
    where it causes problems?

    <html>
    <head>
    <script>documen t.writeln("<div id='mydiv'>stuf f</div>");</script>
    </head>
    <body>blah</body>
    <html>

    cheers,
    Ignac
  • Michael Winter

    #2
    Re: inline document.write( html) within &lt;head&gt; tag safe?

    Ignac Vucko wrote:
    [color=blue]
    > Is writing a document *during* page load safe
    > and supported for all 4th and 5th generation browsers?[/color]

    Considering /all/ modern browsers is rather difficult. However, of the
    common ones that support client-side scripting, yes.

    [snip]
    [color=blue]
    > <script>documen t.writeln("<div id='mydiv'>stuf f</div>");</script>[/color]

    Whilst that is syntactically legal, it would create an invalid
    document tree: DIV elements cannot be descendants of the HEAD element.

    [snip]

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Ivo

      #3
      Re: inline document.write( html) within &lt;head&gt; tag safe?

      "Ignac Vucko" wrote[color=blue]
      > Is writing a document *during* page load safe
      > and supported for all 4th and 5th generation browsers?[/color]

      Almost by definition, writing a document IS a page load. Invoking a
      document.write( ) at any time after page load destroys the current page, in
      other words: during page load is the only time that you can safely use it if
      what you want is to add to the current document.
      [color=blue]
      > If not, can you show me a specific example/browser
      > where it causes problems?
      >
      > <html>
      > <head>
      > <script>documen t.writeln("<div id='mydiv'>stuf f</div>");</script>[/color]

      A <div> element in the head? That is a problem of illegal, not to say
      impossible, nesting. You can document.write( ) all sorts of <link> or <meta>
      elements in the head, but surely your <div> element belongs in the body.
      [color=blue]
      > </head>
      > <body>blah</body>
      > <html>[/color]

      hth
      --
      Ivo



      Comment

      • ignacvucko@gmail.com

        #4
        Re: inline document.write( html) within &lt;head&gt; tag safe?

        thanks for your responses!
        [color=blue][color=green]
        >> A <div> element in the head? That is a problem of illegal,
        >> not to say impossible, nesting.[/color][/color]

        This is what I thought, until I tried it.
        I tested with >=Opera5, >=IE4, >=NN4, Firefox
        ....and to my amazement all not only accepted
        the syntax, but rendered it properly.
        [color=blue][color=green]
        >> Whilst that is syntactically legal, it would create an invalid
        >> document tree: DIV elements cannot be descendants of the HEAD[/color][/color]
        element.

        It appears that all the browsers I have
        tried so far prepend the written html into
        the <body> tag - rendering a valid doc tree.

        So, my question remains:

        Can anybody tell me or provide me an example
        where doing this would cause *any* problems
        in 4th or 5th generationi browsers?
        (eg: javascript errors, alerts, improper rendering,
        overriting of the actual body, page loading issues,
        ...anything)

        Ignac

        Comment

        • Ivo

          #5
          Re: inline document.write( html) within &lt;head&gt; tag safe?

          <ignacvucko@gma il.com> wrote[color=blue][color=green][color=darkred]
          > >> A <div> element in the head? That is a problem of illegal,
          > >> not to say impossible, nesting.[/color][/color]
          >
          > This is what I thought, until I tried it.
          > I tested with >=Opera5, >=IE4, >=NN4, Firefox
          > ...and to my amazement all not only accepted
          > the syntax, but rendered it properly.
          > It appears that all the browsers I have
          > tried so far prepend the written html into
          > the <body> tag - rendering a valid doc tree.[/color]

          Be that as it may, there is no reason to do it this way, to rely on it. None
          that I can think of. The phrase "rendered it properly" really puzzles me. If
          you want the div in the head, it would be proper for it not to render, as
          head content is not supposed to render at all!

          Browsers are designed to second-guess the author's intention when faced with
          questionable markup. They call that a feature, and the smarter the browser,
          the more careless the author can be. Don't jump into that trap.
          [color=blue]
          > So, my question remains:[/color]

          It has been answered a couple of times now.

          --
          Ivo


          Comment

          • ignacvucko@gmail.com

            #6
            Re: inline document.write( html) within &lt;head&gt; tag safe?

            > Don't jump into that trap.
            Agreed. thanks for responding.

            It is interesting though that the spec itself
            indirectly indicates that such markup is valid.

            (since <title> MUST be in a <head> section)

            Ignac

            Comment

            • RobG

              #7
              Re: inline document.write( html) within &lt;head&gt; tag safe?

              ignacvucko@gmai l.com wrote:[color=blue][color=green]
              >>Don't jump into that trap.[/color]
              >
              > Agreed. thanks for responding.
              >
              > It is interesting though that the spec itself
              > indirectly indicates that such markup is valid.
              > http://www.w3.org/TR/REC-html40/inte....html#h-18.2.4
              > (since <title> MUST be in a <head> section)
              >
              > Ignac
              >[/color]

              I disagree with your assumption. The <head> tag is optional, as
              is the <body> tag. Therefore the example given:

              "<TITLE>Tes t Document</TITLE>
              <P><B>Hello World!</B>"

              Does not mean you can put script inside the head, but that you
              can put script into the HTML element without a head or body tag.

              --
              Rob

              Comment

              • Michael Winter

                #8
                Re: inline document.write( html) within &lt;head&gt; tag safe?

                ignacvucko@gmai l.com wrote:

                [snip]
                [color=blue]
                > It is interesting though that the spec itself
                > indirectly indicates that such markup is valid.
                > http://www.w3.org/TR/REC-html40/inte....html#h-18.2.4
                > (since <title> MUST be in a <head> section)[/color]

                It does not. As Rob said, both the start and end tags of the HEAD,
                BODY, and HTML elements are optional. That doesn't mean to say that
                the elements are optional, but that they don't need to be explicitly
                included.

                <TITLE>Test Document</TITLE>
                <P><B>Hello World!</B>

                When the TITLE element, a HEAD content element, is encountered a user
                agent should infer the presence of HTML and HEAD. When the P element,
                a BODY content elements, is encountered a user agent should infer the
                closure of the HEAD element, and the start of the BODY element.

                I thought this process was described in the specification, but I can't
                seem to find it at the moment.

                Mike

                --
                Michael Winter
                Replace ".invalid" with ".uk" to reply by e-mail.

                Comment

                Working...