Web Analytics Made Easy -
StatCounter cross file variable help needed - CodingForum

Announcement

Collapse
No announcement yet.

cross file variable help needed

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • cross file variable help needed

    I have 1.js, 2.js, test.html.

    at the top of 1.js (outside of any function) I have var myvar="test"
    at the top of 2.js (outside of any function) I have var myvar2

    inside a function in 2.js I have:

    myvar2="test2"

    now inside test.html I can read myvar just fine but no myvar2.

    Why not? Both are declared as global by declaring them outside any function right? If I fill myvar2 at the top of 2.js outside of a function I can read it from test.html. Any help?
    ----------------
    Ben Scofield
    http://www.scofield.cc/pipes/ (under construction)

  • #2
    does test.html actually include both js files?
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

    Comment


    • #3
      you mean does it reference them as a src=? If so then, yes. I can get to the variable content from either file, but only if I fill the variable while declaring it outside a function.
      ----------------
      Ben Scofield
      http://www.scofield.cc/pipes/ (under construction)

      Comment


      • #4
        Without seeing the code in action, I can't see what the problem is at this point.
        My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
        “Minds are like parachutes. They don't work unless they are open”
        “Maturity is simply knowing when to not be immature”

        Comment


        • #5
          var myvar2

          Is not a global variable!
          You need to either make it global (remove var) or return myvar2 (return myvar2) in the function and the call that function in the js1 function you wish to use myvar2.

          .....Willy

          Comment


          • #6
            Willy,

            The var keyword only scopes a variable inside functions. Since (according to Codin_away) his declaration is outside of any function, it's in the global scope.
            My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
            “Minds are like parachutes. They don't work unless they are open”
            “Maturity is simply knowing when to not be immature”

            Comment


            • #7
              further testing shows that if I set the variable in the "global scope" and fill the variable at the same time and THEN reference the variable within a function and fill it with another string and finaly reference it from the html file, it sees what was originaly filled in the global area.

              example:

              2.js
              var myvar2="test"
              function {
              myvar2="no test"
              }


              test.html
              alert(myvar2)

              returns "test"
              ----------------
              Ben Scofield
              http://www.scofield.cc/pipes/ (under construction)

              Comment


              • #8
                Originally posted by beetle
                Willy,

                The var keyword only scopes a variable inside functions. Since (according to Codin_away) his declaration is outside of any function, it's in the global scope.
                Hi Beetle;

                That's not the way I interpruted his post.
                inside a function in 2.js I have: myvar2="test2"
                .....Willy

                Comment


                • #9
                  That's not the way I interpruted his post.
                  right, after declaring the variable outside any function I referenced it that way to fill it.

                  at the top of 2.js (outside of any function) I have var myvar2
                  ----------------
                  Ben Scofield
                  http://www.scofield.cc/pipes/ (under construction)

                  Comment


                  • #10
                    only faults I see if you have an unnamed function you never call and you then assume it assigns a value, eventhought its nor named or called

                    Comment


                    • #11
                      Willy

                      I said his declaration was outiside of any function. Now, he attempts to make an assignment to it from an unnamed function (Garadon is right), which is not going to work.

                      However, even if he didn't declare outside the function, by not using the var keyword from inside a function, his variable isn't properly scoped and would reside in the global namespace anyway. Don't believe me? Try it yourself...
                      Code:
                      function createGlobal()
                      {
                      	blah = "yay";
                      }
                      createGlobal();
                      alert( blah );
                      So, Codin_away, what's with this mystery function?
                      My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
                      “Minds are like parachutes. They don't work unless they are open”
                      “Maturity is simply knowing when to not be immature”

                      Comment


                      • #12
                        my functions are named... I was just typing it up quickly.
                        ----------------
                        Ben Scofield
                        http://www.scofield.cc/pipes/ (under construction)

                        Comment


                        • #13
                          Well, then I'm afraid I can't solve your problem without seeing all this code in action, or a non-summarized version of it.
                          My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
                          “Minds are like parachutes. They don't work unless they are open”
                          “Maturity is simply knowing when to not be immature”

                          Comment


                          • #14
                            I know... But I'm dealing with copyright material in 3 files and 1700 lines of code. Worst is I'm trying to modify someone elses stuff that I bought.

                            BTW, Nice website beetle...
                            ----------------
                            Ben Scofield
                            http://www.scofield.cc/pipes/ (under construction)

                            Comment


                            • #15
                              Hi Beetle;

                              Yes, I understand what you are saying. But it is my impression that the O/P has defined a global variable but is trying to redifine the same variable value once a function has executed. In which event, the use of the keyword var would not change the global variable value.
                              Code:
                              <script type="text/javascript">
                              var myvar2="Original Global Variable Two"
                              function alertGlobal(){
                              alert(myvar2)
                              }
                              function non_Global_function(){
                              // NON-GLOBAL VARIABLE //
                              var myvar2="Variable Two NOT Global in function"
                              alert(myvar2)
                              }
                              
                              function Global_function(){
                              // GLOBAL VARIABLE //
                              myvar2="New Variable 2 Value\n(Variable Two Global in function\)"
                              alert(myvar2)
                              }
                              </script>
                              
                              <a href="#" onclick="alertGlobal()">Alert Global Variable</a><br>
                              <a href="#" onclick="non_Global_function()">Alert non_Global_function</a><br>
                              <a href="#" onclick="Global_function()">Global_function</a>
                              If you want to change the value of the global variable,
                              don't use the keyword within the function.

                              If you do not wish to change the defined value of the global value,
                              use the keyword within the function.



                              .....Willy

                              Comment

                              Working...
                              X