Scope of Js variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nileshNP
    New Member
    • Feb 2009
    • 9

    Scope of Js variable

    I have a scenario.

    File A.vxml

    <script src=.".. /test.js">
    var abc={};


    test.js // No declaration of abc in this js file.

    abc['hello']=["Hello"];

    If I compile A.vxml, it is not able to refer to the abc variable from the js file.
    I do not want to declare the abc variable in the JS file.

    Is there any way I can get this thing working?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I don't understand the problem. If you don't want abc in the JS file and the JS file isn't used during compilation anyway, just leave abc out?

    Comment

    • nileshNP
      New Member
      • Feb 2009
      • 9

      #3
      Yes may be I didn't write problem clearly.

      The JS file contains the variable arrary.. as in ..
      abc['LINK']=["Link"];
      abc["IMG']=[" Image is clicked"];
      ....
      ...........
      and this array will be called from various places in the package.

      There is this file A.vxml which gets loaded once our project is started .. as in .. you can say that this is the first file which gets loaded into RAM once the project starts. So I am including the JS file in this file ( with use of <src> tag) so that JS file gets loaded too ..because it will be used by many other programs.

      Everything is working fine If i declare the abc variable in the JS file itself.
      I included the JS file in A.vxml (using src tag) and write just the declaration part in the vxml file. the usage is in the JS file and the declaration is in vxml file.

      Now when some part of the program, when calls abc[IMG] is unable to retrieve the information in that entry(The image is clicked).

      Does this make sense? I tried to explain the problem .. hope it helps. Kindly provide me with some suggestion.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I think I got it.

        well, if you (re)declare abc in the script tag in .vxml, you're effectively overwriting the variable abc in the included JS file (both variables have the same (= global) scope).

        Comment

        • nileshNP
          New Member
          • Feb 2009
          • 9

          #5
          Thank for the reply :)
          But I am not redeclaring it. Just declaring the abc variable in the vxml file.
          There is NO declaration in the js file.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            if you use abc in the JS file (whether it is abc.link or anything else) you have it declared in the first statement that uses abc.

            Comment

            • nileshNP
              New Member
              • Feb 2009
              • 9

              #7
              You mean the there is no way I can do it without declaration of abc in the JS File?

              On the first place, can we include JS scripts in any format file? as in vxml,java... is JS so robust??

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by nileshNP
                You mean the there is no way I can do it without declaration of abc in the JS File?
                you should always declare the variables you use. not doing so could be a source of problems and errors (e.g. the variable gets the wrong scope).
                Originally posted by nileshNP
                On the first place, can we include JS scripts in any format file? as in vxml,java... is JS so robust??
                as long as the JS code block is marked as such and the format file allows it, why not? (JS code is text until a user agent that is capable of JS parses it). file format validaty is something different, though.

                Comment

                • nileshNP
                  New Member
                  • Feb 2009
                  • 9

                  #9
                  Thank you . That was quick :)
                  Originally posted by Dormilich
                  you should always declare the variables you use. not doing so could be a source of problems and errors (e.g. the variable gets the wrong scope).
                  Exactly, that's totally logical. Let me show you what I am doing in here.

                  A.vxml

                  <vxml>
                  [statement A]
                  <Script language="Javas cript" src="External js file> </script>

                  [[statement B]
                  var abc={};

                  [statement C]
                  <prompt><valu e expr="abc[xyz]"/></prompt>

                  </vxml>


                  External JS file

                  abc['pqr']=["Say pqr"];
                  abc['xyz']=["Say xyz"];


                  Now when I execute my A,vxml, the reference to abc[xyz] in one of the prompt doesn't really reach the external js file I don't understand why it should not. Because, I am loading the entire js file using src command before calling any of the js file variable.
                  I have tried rearranging the statements A,B and C. But nothing seems to work.
                  My basic goal is to declare abc in vxml file and use define it in the external js file.

                  Question :
                  1) Is it first of all logical?
                  2) How much difference will it make if I declare it in the js file itself ( which is working)

                  as long as the JS code block is marked as such and the format file allows it, why not? (JS code is text until a user agent that is capable of JS parses it). file format validaty is something different, though.
                  format file allows it? Here you mean, if vxml allows it? How do I know if a format allows js code or not?

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by nileshNP
                    format file allows it? Here you mean, if vxml allows it? How do I know if a format allows js code or not?
                    that's part of the manual/specs. You have to look it up.

                    Comment

                    • nileshNP
                      New Member
                      • Feb 2009
                      • 9

                      #11
                      Hey - I have asked questions in the "quote" block too. Didn't quite knew what a quote block is untill I submitted the query. Please answer them.

                      I will look up the specs.

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        Originally posted by nileshNP
                        Now when I execute my A,vxml, the reference to abc[xyz] in one of the prompt doesn't really reach the external js file I don't understand why it should not. Because, I am loading the entire js file using src command before calling any of the js file variable.
                        the included JS is treated more like a library, where the (in the vxml file) called functions look up the source code (kind of... simplified)

                        Originally posted by nileshNP
                        My basic goal is to declare abc in vxml file and use define it in the external js file.
                        is there a good reason for that? I think it's best to declare the variables, where you use them (so the script will work as a whole).

                        Originally posted by nileshNP
                        2) How much difference will it make if I declare it in the js file itself ( which is working)
                        since it works.... it makes all the difference in the world. (and besides that, it's much easier and cleaner)

                        Comment

                        • nileshNP
                          New Member
                          • Feb 2009
                          • 9

                          #13
                          [QUOTE]
                          Originally posted by Dormilich
                          the included JS is treated more like a library, where the (in the vxml file) called functions look up the source code (kind of... simplified)

                          Can say so.
                          is there a good reason for that? I think it's best to declare the variables, where you use them (so the script will work as a whole).
                          Well my manager says that in future, we will be going to create many js files as such. Present one is for english language. Many other language's js files to come. I really don't understand how will it make a difference if you declare here or there. May be memory space??

                          Since it works.... it makes all the difference in the world. (and besides that, it's much easier and cleaner)
                          Yes it is.

                          Well need to figure out a way of doing it. Else just continue with my old approach which is simple and as efficient as other.

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            Originally posted by nileshNP
                            Well my manager says that in future, we will be going to create many js files as such. ... I really don't understand how will it make a difference if you declare here or there. May be memory space??
                            not that I'm aware of. but a variable declaration is not a big deal (IMO) and it can save you much trouble declaring it in the JS file (as you said).

                            besides that it makes debugging easier...

                            Comment

                            • nileshNP
                              New Member
                              • Feb 2009
                              • 9

                              #15
                              Should not be a big deal but I am finding it to be :(
                              Last try today

                              Comment

                              Working...