id names are global variables?

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

    id names are global variables?

    When I do this:

    <input id="someName" type="text">

    <script type="text/javascript">
    alert(someName. nodeName);
    </script>

    Both IE6 and Firefox alert("INPUT"). Why isn't "someName" undefined?

    Why am I not required to do something like this:

    getElementById( "someName").nod eName;
  • Thomas 'PointedEars' Lahn

    #2
    Re: id names are global variables?

    Bruce wrote:
    <input id="someName" type="text">
    >
    <script type="text/javascript">
    alert(someName. nodeName);
    </script>
    >
    Both IE6 and Firefox alert("INPUT"). Why isn't "someName" undefined?
    >
    Why am I not required to do something like this:
    >
    getElementById( "someName").nod eName;
    Backwards compatibility and maybe compatibility to one another. In Firefox
    it would work only because you are triggering Quirks Mode by omitting the
    DOCTYPE declaration or providing an incomplete one.

    <http://validator.w3.or g/>


    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

    Comment

    • Bruce

      #3
      Re: id names are global variables?

      On Oct 2, 2:29 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
      wrote:
      >
      Backwards compatibility and maybe compatibility to one another.  In Firefox
      it would work only because you are triggering Quirks Mode by omitting the
      DOCTYPE declaration or providing an incomplete one.
      Yes, you're right. I forgot the DOCTYPE. That fixes the problem for
      Firefox but what a nuisance that the problem persists in IE6.

      Comment

      • Jeremy J Starcher

        #4
        Re: id names are global variables?

        On Thu, 02 Oct 2008 12:45:19 -0700, Bruce wrote:
        Yes, you're right. I forgot the DOCTYPE. That fixes the problem for
        Firefox but what a nuisance that the problem persists in IE6.
        IE6, or rather, all versions of IE to date, are a pain. Some browsers
        even go as far as duplicating IE bugs^w features.

        My recommendation: Don't pollute the global name space.

        All class/instance variables should be declared locally. If, for some
        reason, you need two functions to share a group of variables, wrap the
        two functions up together.

        If you MUST use globals, use just one. Make an object and set properties.

        <html>
        <head>
        <title>Test</title>
        </head>
        <body>

        <script type="text/javascript">

        var jjsGlobal = {}; // One variable to rule them all

        function w(s)
        {
        document.write( s + "<br>");
        }

        (function() {
        var onlySeenHere = 0;;

        function test1() {
        onlySeenHere++; ;
        }

        function test2() {
        onlySeenHere++; ;
        }

        test1();
        test2();
        w("OnlySeenHe re = " + onlySeenHere);

        jjsGlobal.test = "Test is set.";

        })();


        w("And our global == " + jjsGlobal.test) ;


        </script>

        </body>
        </html>

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: id names are global variables?

          Bruce wrote:
          Thomas 'PointedEars' Lahn wrote:
          >Backwards compatibility and maybe compatibility to one another. In Firefox
          >it would work only because you are triggering Quirks Mode by omitting the
          >DOCTYPE declaration or providing an incomplete one.
          >
          Yes, you're right. I forgot the DOCTYPE. That fixes the problem for
          Firefox but what a nuisance that the problem persists in IE6.
          Which problem?

          BTW, there are no classes in these languages:
          <http://developer.mozil la.org/en/Core_JavaScript _1.5_Guide/Class-Based_vs._Proto type-Based_Languages >
          <http://javascript.croc kford.com/inheritance.htm l>


          PointedEars
          --
          Anyone who slaps a 'this page is best viewed with Browser X' label on
          a Web page appears to be yearning for the bad old days, before the Web,
          when you had very little chance of reading a document written on another
          computer, another word processor, or another network. -- Tim Berners-Lee

          Comment

          • Bruce

            #6
            Re: id names are global variables?

            On Oct 2, 3:03 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
            wrote:
            Bruce wrote:
            >
            Yes, you're right. I forgot the DOCTYPE. That fixes the problem for
            Firefox but what a nuisance that the problem persists in IE6.
            >
            Which problem?
            The problem with ids also being global variables.

            Comment

            • Bruce

              #7
              Re: id names are global variables?

              On Oct 2, 3:00 pm, Jeremy J Starcher <r3...@yahoo.co mwrote:
              >
              My recommendation:  Don't pollute the global name space.
              Of course but I haven't found a better way yet. I'm writing a simple
              spreadsheet like calculator.

              I want to do the following:

              <input id="a1" type="input"+
              <input id="b1" type="input"=
              <input id="c1" type="input" readonly>

              Then pass the following to a "class"

              "c1 = a1 + b1"

              I want to use eval() to do the math. I want to keep 2 copies of the
              variables. The displayed value is kept in the input form field, and a
              higher precision value is kept in a variable matching the name of the
              input form field.

              I want to check to see if the variable has been defined. If not I
              create it in the global namespace and set it to a default value. With
              IE6 the variables are already defined as objects because of the input
              form ids so it doesn't work.

              In this simple example, if I do this:

              var a1;
              var b1;

              It works, but for larger spreadsheets, I don't want to have to do
              that. I want to do it automatically. For every input form id I find, I
              want to create a global variable so that the simple math string will
              work in an eval()

              Comment

              • Gregor Kofler

                #8
                Re: id names are global variables?

                Bruce meinte:
                On Oct 2, 3:03 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
                wrote:
                >Bruce wrote:
                >>
                >>Yes, you're right. I forgot the DOCTYPE. That fixes the problem for
                >>Firefox but what a nuisance that the problem persists in IE6.
                >Which problem?
                >
                The problem with ids also being global variables.
                Since you hardly ever need global variables, there is no spoon^Wproblem.

                Gregor


                --
                http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
                http://web.gregorkofler.com ::: meine JS-Spielwiese
                http://www.image2d.com ::: Bildagentur für den alpinen Raum

                Comment

                • Jeremy J Starcher

                  #9
                  Re: id names are global variables?

                  On Thu, 02 Oct 2008 13:45:54 -0700, Bruce wrote:
                  On Oct 2, 3:00 pm, Jeremy J Starcher <r3...@yahoo.co mwrote:
                  >>
                  >My recommendation:  Don't pollute the global name space.
                  >
                  Of course but I haven't found a better way yet. I'm writing a simple
                  spreadsheet like calculator.
                  >
                  I want to do the following:
                  >
                  <input id="a1" type="input"+
                  <input id="b1" type="input"=
                  <input id="c1" type="input" readonly>
                  >
                  Then pass the following to a "class"
                  >
                  "c1 = a1 + b1"
                  >
                  I want to use eval() to do the math. I want to keep 2 copies of the
                  variables. The displayed value is kept in the input form field, and a
                  higher precision value is kept in a variable matching the name of the
                  input form field.
                  >
                  I want to check to see if the variable has been defined. If not I create
                  it in the global namespace and set it to a default value. With IE6 the
                  variables are already defined as objects because of the input form ids
                  so it doesn't work.
                  >
                  In this simple example, if I do this:
                  >
                  var a1;
                  var b1;
                  >
                  It works, but for larger spreadsheets, I don't want to have to do that.
                  I want to do it automatically. For every input form id I find, I want to
                  create a global variable so that the simple math string will work in an
                  eval()
                  No global variables! Not here! In other posts I demonstrated how to
                  avoid using global variables.

                  You don't want to create a new variable for every field. You _really_
                  don't.

                  Without knowing more about how you are planning making this, I can think
                  of several approaches that would do you MUCH better.

                  Approach 1:
                  Simple, easy to understand but not elegant.

                  2D arrays, [column][row]
                  A-Z would become 0-26, so a10 would become displayedValue[0][10],
                  calculatedValue[0][10]


                  Approach 2:
                  Objects

                  Objects are the perfect tool to store data like this. You would write
                  code like

                  var spreadSheet = {}; // Create a new object.
                  spreadSheet.a10 = {}; // Create a new cell.

                  var cId = "b10";
                  spreadSheet[cId] = {}; // Create a new cell

                  spreadSheet.a10 .displayedValue = 10;
                  spreadSheet.a10 .calcValue = 9.9999999999999 999999999;


                  Your approach hints at a background programming in BASIC or some other
                  earlier language. Languages have moved beyound that.

                  Before you code any more, do some heavy reading on Javascript, Arrays and
                  Objects (incorrectly called associative arrays).




                  Comment

                  • Bruce

                    #10
                    Re: id names are global variables?

                    Your approach hints at a background programming in BASIC or some other
                    earlier language.  Languages have moved beyound that.
                    >
                    Before you code any more, do some heavy reading on Javascript, Arrays and
                    Objects (incorrectly called associative arrays).- Hide quoted text -
                    I'm not doing it this way because I'm clueless and I like global
                    variables, nor am I new to Javascript or to Structured Programming or
                    Object Oriented programming.

                    I'm trying to keep my equations as simple as "c1 = a1 + b1" just like
                    in an Excel spreadsheet. The simplest way that I've thought of so far
                    is to let a1, b1, c1 be global variables so that eval("c1 = a1 + b1")
                    will work.

                    I don't want my equations to look like this:

                    eval("myObj.c1 = myObj.a1 = myObj.b1");

                    Or

                    eval("myArray[0][2] = myArray[0][0] = myArray[0][1]");

                    If I can find a way, short of writing my own equation parser, to do
                    this without global variables, I'm all for it.

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: id names are global variables?

                      Bruce wrote:
                      Thomas 'PointedEars' Lahn wrote:
                      >Bruce wrote:
                      >>Yes, you're right. I forgot the DOCTYPE. That fixes the problem for
                      >>Firefox but what a nuisance that the problem persists in IE6.
                      >Which problem?
                      >
                      The problem with ids also being global variables.
                      Why is that a problem?


                      PointedEars
                      --
                      Anyone who slaps a 'this page is best viewed with Browser X' label on
                      a Web page appears to be yearning for the bad old days, before the Web,
                      when you had very little chance of reading a document written on another
                      computer, another word processor, or another network. -- Tim Berners-Lee

                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: id names are global variables?

                        Gregor Kofler wrote:
                        Bruce meinte:
                        >Thomas 'PointedEars' Lahn wrote:
                        >>Bruce wrote:
                        >>>Yes, you're right. I forgot the DOCTYPE. That fixes the problem for
                        >>>Firefox but what a nuisance that the problem persists in IE6.
                        >>Which problem?
                        >The problem with ids also being global variables.
                        >
                        Since you hardly ever need global variables, there is no spoon^Wproblem.
                        YMMD :)


                        \\// PointedEars, who's got a Matrix to fix
                        --
                        Use any version of Microsoft Frontpage to create your site.
                        (This won't prevent people from viewing your source, but no one
                        will want to steal it.)
                        -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

                        Comment

                        • Bruce

                          #13
                          Re: id names are global variables?

                          On Oct 2, 4:25 pm, Bruce <batman4...@yah oo.cawrote:
                          I'm trying to keep my equations as simple as "c1 = a1 + b1" just like
                          in an Excel spreadsheet.
                          If I can find a way, short of writing my own equation parser, to do
                          this without global variables, I'm all for it.
                          Thanks to a suggestion from Jeremy J Starcher in another thread, I
                          found my solution.

                          Instead of global variables, I create variables within my "class" like
                          this:

                          this["a1"] = 1;
                          this["b1"] = 2;

                          then I can do this:

                          with(this) {
                          c1 = eval("a1 + b1");
                          }

                          Problem solved. No global variables and I get to keep my simple
                          equations.

                          Comment

                          • Thomas 'PointedEars' Lahn

                            #14
                            Re: id names are global variables?

                            Bruce wrote:
                            On Oct 2, 4:25 pm, Bruce <batman4...@yah oo.cawrote:
                            >I'm trying to keep my equations as simple as "c1 = a1 + b1" just like
                            >in an Excel spreadsheet.
                            Excel is certainly not a programming reference.
                            >If I can find a way, short of writing my own equation parser, to do
                            >this without global variables, I'm all for it.
                            >
                            Thanks to a suggestion from Jeremy J Starcher in another thread, I
                            found my solution.
                            >
                            Instead of global variables, I create variables within my "class" like
                            this:
                            >
                            this["a1"] = 1;
                            this["b1"] = 2;
                            this.a1 = 1;
                            this.b1 = 2;

                            is equivalent since the property names are identifiers.
                            then I can do this:
                            >
                            with(this) {
                            c1 = eval("a1 + b1");
                            You rarely ever need eval(), and you certainly don't need it here:

                            c1 = a1 + b1;

                            or

                            c1 = this.a1 + this.b1;
                            }
                            >
                            Problem solved. No global variables and I get to keep my simple
                            equations.
                            No, you cannot if the data comes from form controls.

                            However, you could use a form (here: referred to by the `form' property of
                            the calling object) directly, whereas `a1' or `b1' are IDs or names of form
                            controls (with the latter being better supported):

                            with (this.form.elem ents)
                            {
                            c1 = parseFloat(a1.v alue) + parseFloat(b1.v alue);
                            }

                            or without the `with' statement:

                            var es = this.form.eleme nts;
                            var a1 = es["a1"].value;
                            var b1 = es["b1"].value;
                            var c1 = parseFloat(a1) + parseFloat(b1);


                            PointedEars
                            --
                            var bugRiddenCrashP ronePieceOfJunk = (
                            navigator.userA gent.indexOf('M SIE 5') != -1
                            && navigator.userA gent.indexOf('M ac') != -1
                            ) // Plone, register_functi on.js:16

                            Comment

                            • Bruce

                              #15
                              Re: id names are global variables?

                              On Oct 2, 8:22 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
                              wrote:
                              Bruce wrote:
                              On Oct 2, 4:25 pm, Bruce <batman4...@yah oo.cawrote:
                              I'm trying to keep my equations as simple as "c1 = a1 + b1" just like
                              in an Excel spreadsheet.
                              >
                              Excel is certainly not a programming reference.
                              I didn't make a programming reference to Exel. I made a reference to
                              the way equations are entered into Excel.

                              I'm writing a general purpose "class" where I can configure it with
                              strings of equations using form id names as variable names that will
                              update other form fields as the input data changes. I want my
                              "spreadshee t" calculation to be as unobtrusive as possible so that I
                              create a form with id names for variable names and my "class"
                              automates the rest (adds "onchange" events, initializes form field
                              values ...).
                              >
                              this["a1"] = 1;
                              this["b1"] = 2;
                              >
                                this.a1 = 1;
                                this.b1 = 2;
                              I have to do

                              this["a1"] = 1

                              because I pass variable names to my "class" as string names. I want to
                              configure the class with equations in the form of strings. I will pass
                              something like "c1 = a1 + b1" as a string to my class.

                              So I'm actually doing something like this (as a trivial example):

                              setDefaults : function(variab leNames,default Value) {
                              for (var i = 0; i < variableNames.l ength; i++) {
                              this[variableNames[i]] = defaultValue;
                              }
                              }
                              You rarely ever need eval(), and you certainly don't need it here:
                              >
                                c1 = a1 + b1;
                              >
                              or
                              >
                                c1 = this.a1 + this.b1;
                              Can't. I'm passing variable names as strings. I'm passing equations as
                              strings. So, yes I need eval() or I need to write my own equation
                              parser. Eval() already does that for me so I might as well use it.
                              Problem solved. No global variables and I get to keep my simple
                              equations.
                              >
                              No, you cannot if the data comes from form controls.
                              I completely control the equations. Users do not input them so they
                              won't be able to inject unwanted Javascript.

                              My application will not look like a spreadsheet. It will look like a
                              form where users input some data and I perform preconfigured math on
                              the data and display things like a running total in real time. Since I
                              want this code to be re-useable on other web sites, I'll configure the
                              code with an array of equations and variable names as strings and use
                              eval() to perform the math.

                              Comment

                              Working...