document.write issue

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

    #1

    document.write issue

    Hi,

    I have the following script:

    -----------------------------------------------------------------------------------
    <script type="text/javaScript">
    <!--
    document.write( '<div id=hello1>Hello 1</div>');
    document.write( '<div id=hello2 style="display: none;"><script
    src="test.js">< \/script></div>');
    //-->
    </script>

    where test.js contains one line: document.write( "Hello2");
    -----------------------------------------------------------------------------------

    The intent is to only show the first div and hide the sencond div until late
    to display if needed.
    The result, however, shown both div contents.

    If I change the script to:
    -----------------------------------------------------------------------------------
    <script type="text/javaScript">
    <!--
    document.write( '<div id=hello1>Hello 1</div>');
    document.write( '<div id=hello2
    style="display: none>document.w rite("Hello2"); (</div>');
    //-->
    </script>
    -----------------------------------------------------------------------------------
    It works fine.

    If I further change it to:
    -----------------------------------------------------------------------------------
    <script type="text/javaScript">
    <!--
    document.write( '<div id=hello1>Hello 1</div>');
    //-->
    </script>

    <script type="text/javaScript">doc ument.write('<d iv id=hello2
    style="display: none>');</script>
    <script type="text/javaScript">doc ument.write('<s cript
    src="test.js">< \/script>');</script>
    <script type="text/javaScript">doc ument.write('</div>');</script>
    -----------------------------------------------------------------------------------
    it also works fine.

    Any help is highly appreciated.

    Sean




  • kaeli

    #2
    Re: document.write issue

    In article <msAee.1237937$ 6l.256275@pd7tw 2no>, snorlaxnet@hotm ail.com
    enlightened us with...[color=blue]
    > Hi,
    >
    > I have the following script:
    >[/color]

    Out of curiousity, why are you using the rather outdated document.write to
    add new DOM elements?
    Do you need to support old browsers?

    You can add script elements and DIVs with DOM methods.

    That said, I seem to recall a problem with document.write and writing the tag
    "</script>"
    I think you need to split it, like
    document.write( "</scr"+"ipt>");
    Something about the parser interpreting it as a real </script>...?

    --
    --
    ~kaeli~
    When two egotists meet, it's an I for an I.



    Comment

    • Craig Jurney

      #3
      Re: document.write issue

      kaeli wrote:
      [color=blue]
      > In article <msAee.1237937$ 6l.256275@pd7tw 2no>, snorlaxnet@hotm ail.com
      > enlightened us with...
      >[color=green]
      >>Hi,
      >>
      >>I have the following script:
      >>[/color]
      >
      >
      > Out of curiousity, why are you using the rather outdated document.write to
      > add new DOM elements?
      > Do you need to support old browsers?
      >
      > You can add script elements and DIVs with DOM methods.
      >
      > That said, I seem to recall a problem with document.write and writing the tag
      > "</script>"
      > I think you need to split it, like
      > document.write( "</scr"+"ipt>");
      > Something about the parser interpreting it as a real </script>...?
      >[/color]

      Exactly so. Cf. David Flanagan's "JavaScript , the Definitive Guide"
      12.2.1.2 [p.214, 3rd Ed.], [p.188, 4th Ed.]

      "...[T]he HTML parser makes make no attempt to understand your
      JavaScript code, and if it sees the string "</script>" in your code,
      even if it appears within quotes, it assumes that it has found the
      closing tag of the currently running script."

      /Craig Jurney

      Comment

      • RobB

        #4
        Re: document.write issue

        Craig Jurney wrote:[color=blue]
        > kaeli wrote:[/color]

        (snip)
        [color=blue][color=green]
        > > That said, I seem to recall a problem with document.write and[/color][/color]
        writing the tag[color=blue][color=green]
        > > "</script>"
        > > I think you need to split it, like
        > > document.write( "</scr"+"ipt>");
        > > Something about the parser interpreting it as a real </script>...?
        > >[/color]
        >
        > Exactly so.[/color]

        (snip)

        Exactly not. This:

        document.write( '<div...><scrip t src="test.js">< \/script></div>­');

        ....will prevent the closing tag from being parsed just as effectively
        (and more compactly).

        No idea what the issue is here but it seems like a pretty obscure one.

        Comment

        • Sean

          #5
          Re: document.write issue

          Thanks for the reply.

          I have an escape for the script tag: <\/script> to avoid the problem you
          mentioned but still have the wrong result.

          Could you show an example of DOM method you mentioned?

          Sean

          "kaeli" <tiny_one@NOSPA M.comcast.net> wrote in message
          news:MPG.1ce522 e929a3d6e198a58 4@nntp.lucent.c om...[color=blue]
          > In article <msAee.1237937$ 6l.256275@pd7tw 2no>, snorlaxnet@hotm ail.com
          > enlightened us with...[color=green]
          >> Hi,
          >>
          >> I have the following script:
          >>[/color]
          >
          > Out of curiousity, why are you using the rather outdated document.write to
          > add new DOM elements?
          > Do you need to support old browsers?
          >
          > You can add script elements and DIVs with DOM methods.
          >
          > That said, I seem to recall a problem with document.write and writing the
          > tag
          > "</script>"
          > I think you need to split it, like
          > document.write( "</scr"+"ipt>");
          > Something about the parser interpreting it as a real </script>...?
          >
          > --
          > --
          > ~kaeli~
          > When two egotists meet, it's an I for an I.
          > http://www.ipwebdesign.net/wildAtHeart
          > http://www.ipwebdesign.net/kaelisSpace
          >[/color]


          Comment

          • kaeli

            #6
            Re: document.write issue

            In article <l2Pee.1250285$ 8l.914640@pd7tw 1no>, snorlaxnet@hotm ail.com
            enlightened us with...[color=blue]
            > Thanks for the reply.
            >
            > I have an escape for the script tag: <\/script> to avoid the problem you
            > mentioned but still have the wrong result.
            >
            > Could you show an example of DOM method you mentioned?[/color]

            Sure.
            I already have one here. Removes / adds a script element as needed for some
            validation routines.


            DIV is pretty similar, depending on what you want to do with it.

            --
            --
            ~kaeli~
            Going to church doesn't make you a Christian any more than
            standing in a garage makes you a car.



            Comment

            • Randy Webb

              #7
              Re: document.write issue

              RobB wrote:
              [color=blue]
              > Craig Jurney wrote:
              >[color=green]
              >>kaeli wrote:[/color]
              >
              >
              > (snip)
              >
              >[color=green][color=darkred]
              >>>That said, I seem to recall a problem with document.write and[/color][/color]
              >
              > writing the tag
              >[color=green][color=darkred]
              >>>"</script>"
              >>>I think you need to split it, like
              >>>document.wri te("</scr"+"ipt>");
              >>>Something about the parser interpreting it as a real </script>...?
              >>>[/color]
              >>
              >>Exactly so.[/color]
              >
              >
              > (snip)
              >
              > Exactly not.[/color]

              No, Exactly.


              [color=blue]
              > This:
              > document.write( '<div...><scrip t src="test.js">< \/script></div>­');[/color]

              That is not the code that was posted. It is the solution to the problem
              though.

              --
              Randy
              comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

              Comment

              • cwdjrxyz@yahoo.com

                #8
                Re: document.write issue

                Anytime you use a document.write, it is a good idea to escape-backslash
                all closing tags contained within the document.write. Such tags may
                include <\/a>, <\/p>, <\/div> etc. Usually not backslashing causes no
                trouble other than making the W3C validator grumble when it sees it.
                However, in a few cases, such as the <\/script> being discussed in this
                thread, you can have real problems that can be very difficult to find.
                I have wasted enough time in the past running down hard-to-find errors
                that were caused by the lack of backslash to make me always backslash
                closing tags in document.write althogh you often can get away without
                doing so. On the other hand, I have never had a problem that was caused
                by backslashing closing tags in a document.write.

                Comment

                • cwdjrxyz@yahoo.com

                  #9
                  Re: document.write issue

                  Anytime you use a document.write, it is a good idea to escape-backslash
                  all closing tags contained within the document.write. Such tags may
                  include <\/a>, <\/p>, <\/div> etc. Usually not backslashing causes no
                  trouble other than making the W3C validator grumble when it sees it.
                  However, in a few cases, such as the <\/script> being discussed in this
                  thread, you can have real problems that can be very difficult to find.
                  I have wasted enough time in the past running down hard-to-find errors
                  that were caused by the lack of backslash to make me always backslash
                  closing tags in document.write althogh you often can get away without
                  doing so. On the other hand, I have never had a problem that was caused
                  by backslashing closing tags in a document.write.

                  Comment

                  • RobB

                    #10
                    Re: document.write issue

                    Randy Webb wrote:[color=blue]
                    > RobB wrote:
                    >[color=green]
                    > > Craig Jurney wrote:
                    > >[color=darkred]
                    > >>kaeli wrote:[/color]
                    > >
                    > >
                    > > (snip)
                    > >
                    > >[color=darkred]
                    > >>>That said, I seem to recall a problem with document.write and[/color]
                    > >
                    > > writing the tag
                    > >[color=darkred]
                    > >>>"</script>"
                    > >>>I think you need to split it, like
                    > >>>document.wri te("</scr"+"ipt>");
                    > >>>Something about the parser interpreting it as a real </script>...?
                    > >>>
                    > >>
                    > >>Exactly so.[/color]
                    > >
                    > >
                    > > (snip)
                    > >
                    > > Exactly not.[/color]
                    >
                    > No, Exactly.
                    >
                    >
                    >[color=green]
                    > > This:
                    > > document.write( '<div...><scrip t src="test.js">< \/script></div>­');[/color]
                    >
                    > That is not the code that was posted. It is the solution to the[/color]
                    problem[color=blue]
                    > though.
                    >
                    > --
                    > Randy
                    > comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup[/color]
                    weekly

                    You're correct. This:
                    [color=blue][color=green]
                    > > document.write( '<div...><scrip t src="test.js">< \/script></div>­');[/color][/color]

                    "is not the code that was posted..."

                    I did this thing...called "abbreviati on".

                    *This* is the code posted originally (with closing script tags as
                    strings):

                    [1]

                    <script type="text/javaScript">
                    <!--
                    document.write( '<div id=hello1>Hello 1</div>');
                    document.write( '<div id=hello2 style="display: none;"><script
                    src="test.js">< \/script></div>­');
                    //-->
                    </script>

                    [2]

                    <script type="text/javaScript">
                    <!--
                    document.write( '<div id=hello1>Hello 1</div>');
                    //-->
                    </script>


                    <script type="text/javaScript">doc umen­t.write('< div id=hello2
                    style="display: none>');</scrip­t>
                    <script type="text/javaScript">doc umen­t.write('< script
                    src="test.js">< \/script>');</s­cript>
                    <script type="text/javaScript">doc umen­t.write('</div>');</script>

                    As the OP noted:
                    [color=blue]
                    > I have an escape for the script tag: <\/script> to
                    > avoid the problem you mentioned [..][/color]

                    What are you talking about?

                    Comment

                    • cwdjrxyz@yahoo.com

                      #11
                      Re: document.write issue

                      Not backslashing closing tags in a document.write is an error, not just
                      an option, as you will find if you validate your page at the W3C. Thus
                      your closing division tags within a document.write all should be
                      backslashed. The following quote explains this.

                      "A common error (and the most common source of erroneous bug reports
                      for the WDG HTML Validator) occurs when writing HTML tags within a
                      SCRIPT element:

                      <script type="text/javascript">
                      <!--
                      // This is an error!
                      document.write( "</P>");
                      // -->
                      </script>
                      As mentioned in the HTML 4 Recommendation' s note about specifying
                      non-HTML data in element content, end tags are recognized within SCRIPT
                      elements, but other kinds of markup--such as start tags and
                      comments--are not. This is an unintuitive quirk of SGML for elements
                      defined to have CDATA content.

                      Authors should avoid using strings such as "</P>" in their embedded
                      scripts. In JavaScript, authors may use a backslash to prevent the
                      string from being parsed as markup:

                      <script type="text/javascript">
                      <!--
                      document.write( "<\/P>");
                      // -->
                      </script>
                      Note that in XHTML, authors must also take care when using start tags
                      within a script element. For details, see the Script and Style elements
                      section of the XHTML 1.0 Recommendation as well as the HTML
                      compatibility guideline for embedded scripts."

                      Of course not backslashing the closing division tags may have had
                      nothing to do with your problem. However, I find it much easier to find
                      errors if I make the code valid first. I had a problem with not
                      backslashing closing division tags that were nested in a document.write
                      several years ago, and it took me hours to find the reason.

                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: document.write issue

                        Craig Jurney wrote:
                        [color=blue]
                        > kaeli wrote:[color=green]
                        >> [...] snorlaxnet@hotm ail.com enlightened us with...
                        >> That said, I seem to recall a problem with document.write and writing the
                        >> tag "</script>"
                        >> I think you need to split it, like
                        >> document.write( "</scr"+"ipt>");
                        >> Something about the parser interpreting it as a real </script>...?[/color]
                        >
                        > Exactly so. Cf. David Flanagan's "JavaScript , the Definitive Guide"
                        > 12.2.1.2 [p.214, 3rd Ed.], [p.188, 4th Ed.]
                        >
                        > "...[T]he HTML parser makes make no attempt to understand your[/color]
                        ^^^^^^^^^^^[color=blue]
                        > JavaScript code, and if it sees the string "</script>" in your code,
                        > even if it appears within quotes, it assumes that it has found the
                        > closing tag of the currently running script."[/color]

                        However, Flanagan is wrong again. If we are talking about HTML parsers
                        (and not those tag soup parsers implemented in desktop Web browsers),
                        `"</scr" + "ipt>"' and the like won't help. With a (strict) HTML parser,
                        it is the ETAGO (End Tag Open) delimiter (`</') within the script that
                        causes the problem. The `script' element contains CDATA (character data),
                        and an SGML compliant parser, as a strict HTML parser would be, parsers
                        CDATA content up to the ETAGO delimiter. That is why the script engine is
                        passed incomplete and therefore syntactical incorrect code.

                        cwdjrxyz@yahoo. com pointed out the correct solution for source code of
                        ECMAScript compliant languages included in HTML documents. Yet the reason
                        why the W3C Markup Validator "grumbles" at the ETAGO delimiter is that an
                        SGML compliant markup parser is used for validation (see the Validator
                        FAQ).

                        BTW: This has been discussed ad nauseam before. Read the cljs FAQ.


                        PointedEars

                        Comment

                        • Thomas 'PointedEars' Lahn

                          #13
                          Re: document.write issue

                          cwdjrxyz@yahoo. com wrote:
                          [color=blue]
                          > <script type="text/javascript">
                          > <!--
                          > document.write( "<\/P>");
                          > // -->
                          > </script>
                          > Note that in XHTML, authors must also take care when using start tags
                          > within a script element.[/color]

                          And because of that, authors SHOULD NOT comment out script content the
                          way it was done above in XHTML, as an XML parser is allowed to remove such
                          content. In HTML, commenting out scripts that way is neither necessary
                          nor reasonable, as it is also error-prone.
                          [color=blue]
                          > For details, see the Script and Style elements
                          > section of the XHTML 1.0 Recommendation as well as the HTML
                          > compatibility guideline for embedded scripts."[/color]

                          The HTML Compatibility Guidelines of the XHTML 1.0 Specification
                          are flawed, for HTML and XHTML SHORTTAG features differ since they
                          are applications of different markup languages (SGML and XML).
                          E.g. `<... />' means a different thing in HTML and XHTML.


                          PointedEars

                          Comment

                          Working...