weird var problem

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

    weird var problem

    Hi everyone,
    I have encountered a small problem concerning global variables:

    var print = function(string ){
    var reference = document.getEle mentById("myDiv ");
    reference.inner HTML += string + "<br/>";
    };

    This doesn't:
    REF = {
    div1 : document.getEle mentById("myDiv ")
    };

    var print = function(string ){
    REF.div1.innerH TML += string + "<br/>";
    };

    Firebug complains that REF.div1.innerH TML is null . Why?

  • Gregor Kofler

    #2
    Re: weird var problem

    disappearedng meinte:
    Hi everyone,
    I have encountered a small problem concerning global variables:
    >
    var print = function(string ){
    var reference = document.getEle mentById("myDiv ");
    reference.inner HTML += string + "<br/>";
    };
    >
    This doesn't:
    REF = {
    div1 : document.getEle mentById("myDiv ")
    };
    >
    var print = function(string ){
    REF.div1.innerH TML += string + "<br/>";
    };
    >
    Firebug complains that REF.div1.innerH TML is null . Why?
    Does myDiv already exist, when REF is being defined? Your first example
    needs the reference only upon being called (perhaps long after the
    document has completed loading), the latter one when REF gets defined -
    presumably somewhere in a script element.

    Gregor

    Comment

    • disappearedng

      #3
      Re: weird var problem

      thx that helped it

      Comment

      • disappearedng

        #4
        Re: weird var problem

        Wait
        If I have an html
        <html>
        <head>
        <script type="text/javascript">
        .... DEFINITION of the javascript here ...
        </script>
        <body>
        <div id="myDiv"/>
        </body>
        </html>

        My question is:
        1) Does Javascript gets loaded before the HTML?
        2) If what you said is indeed true, myDiv not defined, then how come
        when my calling of print inside the Javascipt definition area works
        then? If myDiv is not defined upon compile time, then why will

        var print = function(string ){
        var reference = document.getEle mentById
        ("myDiv");
        reference.inner HTML += string + "<br/>";
        };

        this work since myDiv has not been created (or I misintreprid what you
        said wrong)

        Comment

        • disappearedng

          #5
          Re: weird var problem

          Wait
          If I have an html
          <html>
          <head>
          <script type="text/javascript">
          .... DEFINITION of the javascript here ...
          </script>
          <body>
          <div id="myDiv"/>
          </body>
          </html>

          My question is:
          1) Does Javascript gets loaded before the HTML?
          2) If what you said is indeed true, myDiv not defined, then how come
          when my calling of print inside the Javascipt definition area works
          then? If myDiv is not defined upon compile time, then why will

          var print = function(string ){
          var reference = document.getEle mentById
          ("myDiv");
          reference.inner HTML += string + "<br/>";
          };

          this work since myDiv has not been created (or I misintreprid what you
          said wrong)

          Comment

          • Gregor Kofler

            #6
            Re: weird var problem

            disappearedng meinte:
            Wait
            If I have an html
            <html>
            <head>
            <script type="text/javascript">
            ... DEFINITION of the javascript here ...
            </script>
            <body>
            <div id="myDiv"/>
            </body>
            </html>
            >
            My question is:
            1) Does Javascript gets loaded before the HTML?
            Loaded? No. Or did you mean parsed? Since the scripts are parsed along
            with the rest of the document - it depends.
            2) If what you said is indeed true, myDiv not defined, then how come
            when my calling of print inside the Javascipt definition area works
            then?
            When you call the function, myDiv has come to existence due to the
            complete parsing of your HTML document.

            Gregor

            Comment

            • disappearedng

              #7
              Re: weird var problem

              Dear Greg,
              I don't think you are right
              Please look at what I meant


              <html>
              <head>
              <script type="text/javascript">
              var print = function(string ){
              var reference = document.getEle mentById("myDiv ");
              reference.inner HTML += string + "<br/>";
              };

              /*
              REF = {
              div1 : document.getEle mentById("myDiv ")

              };

              var print = function(string ){
              REF.div1.innerH TML += string + "<br/>";

              };
              */

              function init() {
              print("Hello");
              }

              </script>
              <body onload="init(); ">
              <div id="myDiv"/>
              </body>
              </html>

              Try uncommenting the commented part.
              What you said was not possible because of the following scenario:

              If the javascript is parsed before div was defined, then the top print
              function will not work anyway because what revoked it was init() and
              init() was called by body.

              Both functions are executed at the same time in both scenarios. It
              would be not be possible that in one scenario Div was defined and the
              other not.

              Comment

              • Gregor Kofler

                #8
                Re: weird var problem

                disappearedng meinte:
                Dear Greg,
                I don't think you are right
                Sigh...
                <html>
                <head>
                <script type="text/javascript">
                REF = {
                div1 : document.getEle mentById("myDiv ")
                >
                };
                REF.div1 is now "undefined" . Since the browser hasn't come across the
                div yet.
                ....
                <div id="myDiv"/>
                .... now "myDiv" becomes available. Alas, too late.

                ....

                Much later: The load event fires.
                What you said was not possible because of the following scenario:
                Said to me by someone, who is completely clueless.
                If the javascript is parsed before div was defined, then the top print
                function will not work anyway because what revoked it was init() and
                init() was called by body.
                Revoked? init() would be called once the body is completely loaded. But
                REF.div1 was assigned long before that.

                Gregor
                Both functions are executed at the same time in both scenarios. It
                would be not be possible that in one scenario Div was defined and the
                other not.

                Comment

                • disappearedng

                  #9
                  Re: weird var problem

                  Alright that clear things up.

                  Let me confirm with you
                  1) the "onload="init() ;"" part of my body is executed AFTER the entire
                  HTML has been parsed?
                  2) Hence the second print works because init() is called AFTER myDiv
                  has been created, and when init calls print THEN the javascript parser
                  looks up the already created myDiv element and provides a non null
                  reference?

                  I tried implementing the first print function in init and everything
                  worked. Thank you.

                  It's just that I had no idea that body's onload will only be called
                  when the ENTIRE html has been parsed.

                  Thank you

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: weird var problem

                    disappearedng wrote:
                    1) the "onload="init() ;"" part of my body is executed AFTER the entire
                    HTML has been parsed?
                    No, only after it has been *rendered* also (with some exceptions).
                    2) Hence the second print works because init() is called AFTER myDiv has
                    been created, and when init calls print THEN the javascript parser looks
                    up the already created myDiv element and provides a non null reference?
                    Roughly speaking, yes.

                    What really happens in the majority of cases is: The client-side script
                    source code is parsed into language tokens according to the (modified)
                    ECMAScript grammar, and JIT-compiled into byte-code before execution.

                    The Virtual Machine that interprets (executes) that byte-code program then
                    calls a method of a DOM object that is an implementation of an interface
                    (method), as the result of the compilation. That method cannot return a
                    non-null value (with the meaning of `null' being implementation-dependent)
                    if the DOM object that represents the (HTML) element (an element object) has
                    not been registered in the DOM tree yet.

                    Sometimes element objects are created and registered in the DOM tree when
                    the corresponding element has been parsed. But since the parse tree of the
                    markup parser is incomplete until the last element (usually the `html'
                    element) has been parsed or inferred (because of optional tags), you cannot
                    be sure that the DOM tree is complete either until the `load' event of the
                    document (standardized in the W3C DOM Level 2 Events Specification) occurs.
                    That event can be handled with the intrinsic `onload' event-handler
                    attribute of the `body' element (among other, less reliable methods), as
                    standardized in the HTML 4.01 Specification.


                    PointedEars

                    P.S.: I selected the wrong menu item again, sorry for the e-mail.

                    Comment

                    Working...