Why, please, this strange behavior?

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

    Why, please, this strange behavior?

    Why this script:



    --------------------------------------------------------------
    <div id='1'></div>
    <script language='javas cript'><!--

    function pippo(){
    alert('primo alert');
    var ddiv1 = document.getEle mentById('1');
    ddiv1.innerHTML = '1 elaborato con successo...<br> ';
    alert('secondo alert');
    }
    //--></script>
    <a href="#" onClick="pippo( );">Vai</a>
    --------------------------------------------------------------







    and this one NO?


    --------------------------------------------------------------
    <div id='1'></div>
    <script language='javas cript'><!--
    alert('primo alert');
    var ddiv1 = document.getEle mentById('1');
    ddiv1.innerHTML = '1 elaborato con successo...<br> ';
    alert('secondo alert');
    //--></script>
    --------------------------------------------------------------

    Tested on IE and FF, only first alert works in second case.

    I think because the page, in the second case, is not completely loaded.

    Isn't it?

    And if it is how can I do?

    Any help appreciated.

    Regards.

    --
    Fabri
    (Incredibile come si tenda a credere di piu` a Rossi. (cit.))
  • mscir

    #2
    Re: Why, please, this strange behavior?

    Fabri wrote:
    <snip>[color=blue]
    > I think because the page, in the second case, is not completely loaded.[/color]

    I think that's right. How about using the onload event?

    <script type='text/javascript'>
    function doit(){
    alert('1st alert');
    var ddiv1 = document.getEle mentById('1');
    ddiv1.innerHTML = '1 elaborato con successo...<br> ';
    alert('2nd alert');
    }
    </script>
    </head>

    <body onload="doit()" ;>
    <div id='1'>
    </div>

    Comment

    • Michael Winter

      #3
      Re: Why, please, this strange behavior?

      On Thu, 18 Nov 2004 09:07:35 +0100, Fabri <no@sp.am> wrote:

      [snip]
      [color=blue]
      > and this one NO?
      >
      > --------------------------------------------------------------
      > <div id='1'></div>
      > <script language='javas cript'><!--[/color]

      Minor points:

      1) The language attribute has been deprecated for over six years. Use the
      (required) type attribute instead.

      <script type="text/javascript">

      2) The practice of hiding scripts using SGML comments is also out-of-date.
      You can remove them.
      [color=blue]
      > alert('primo alert');
      > var ddiv1 = document.getEle mentById('1');
      > ddiv1.innerHTML = '1 elaborato con successo...<br> ';
      > alert('secondo alert');
      > //--></script>
      > --------------------------------------------------------------
      >
      > Tested on IE and FF, only first alert works in second case.
      >
      > I think because the page, in the second case, is not completely loaded.
      >
      > Isn't it?[/color]

      I would actually say, no. In my experience, as long as the element in
      question - a DIV here - has been parsed, you should be able to access it
      via a script.

      What could happen in your case is that because the DIV has no content, the
      browser is ignoring it. Try putting something - text, for example - in it
      and try again.

      If you're still having trouble, could you post a full example. I can't
      replicate the problem, so it might be something specific that you're doing.

      Mike

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

      Comment

      • Fabri

        #4
        Re: Why, please, this strange behavior?

        Michael Winter wrote:
        [color=blue]
        > What could happen in your case is that because the DIV has no content,
        > the browser is ignoring it. Try putting something - text, for example -
        > in it and try again.[/color]

        Good Job Michael :-)


        I post to you (and to the group) what did:

        I assume that you know some vbscript.

        <%

        for idx = 1 to 5
        response.Write( "<div id='" & idx & "' style='display: none;'>a</div>")
        next


        arrTest = array("a", "b", "c", "d", "e")

        cnt = 1

        response.Flush( )

        for idx = lbound(arrTest) to ubound(arrTest)

        response.Flush( )
        response.Write( vbcrlf)
        response.Write( "<script language='javas cript'><!--")
        response.Write( vbcrlf)
        response.Write( vbcrlf)
        response.Write( " var ddiv" & cnt & " = document.getEle mentById('" &
        cnt & "');")
        response.Write( vbcrlf)
        response.Write( " ddiv" & cnt & ".innerHTML = '" & cnt & "
        succesfully elaborated...<b r>';")
        response.Write( vbcrlf)
        response.Write( " ddiv" & cnt & ".style.dis play = '';")
        response.Write( vbcrlf)
        response.Write( vbcrlf)
        response.Write( "//--></script>")
        response.Flush( )


        ' Here I try to give some delay to sequentially let the DIVs to be drawed

        for ix = 1 to 100000
        response.Flush( )
        next

        cnt = cnt + 1

        response.Flush( )

        next
        %>



        It works.

        Regards.


        --
        Fabri
        (Incredibile come si tenda a credere di piu` a Rossi. (cit.))

        Comment

        Working...