5 Replies Latest reply on Apr 19, 2012 5:49 AM by linopasc

    Can't execute functions correctly in a button event

    phoenix2ch

      Hi,

       

      I have some troubles with a script in JS, i can't execute correctly the code when i'm use a button event like:

       

      // Change button

      var btn = this.getField('Change');

      btn.setAction("MouseUp", "refreshLayers()");

       

      When i don't use a event and put directly the function in the code, it's work.

       

       

      error return:

      "SyntaxError: missing } in compound statement

      95: à la ligne 95" (line 95)

       

      Here my script, there isn't missing a '}':

       

      function changeAll(sSelected, sCible) {

       

       

                var sName, i, iExceptLength = sCible.length, aOCGs = this.getOCGs();

       

       

                for (i = 0; aOCGs && i < aOCGs.length; i += 1) {

                    sName = aOCGs[i].name;

         

                    if(sCible == sName.substring(0, iExceptLength) && sName != sSelected) {

                                    aOCGs[i].state = false;

                          }

                          else if(sName == sSelected) {

                                    aOCGs[i].state = true;

                          }

          }

       

       

       

       

      }

       

       

       

       

      function refreshLayers() {

       

                var tmp = this.getField('Shape');

                var iCurrentIndex = tmp.currentValueIndices;

                var a = tmp.getItemAt(iCurrentIndex, false);

       

                tmp = this.getField('Color');

                iCurrentIndex = tmp.currentValueIndices;

                var b = tmp.getItemAt(iCurrentIndex, false);

       

                tmp = this.getField('ColorPrice');

                iCurrentIndex = tmp.currentValueIndices;

                var c = tmp.getItemAt(iCurrentIndex, false);

       

       

                changeAll(a+'_'+b, 'Shape-');

                changeAll('Price_'+c, 'Price_');

      }

       

       

       

       

      function inArray(search, a) {

       

       

                var bFinded = false;

                var i;

                for (i = 0; i < a.length && bFinded == false; i++) {

                                    if (a[i] == search)

                                              bFinded = true;

                }

       

                return bFinded;

       

       

      }

       

       

       

       

      function init() {

      // Selectors names

      var sSelectName = 'Shape';

      var sColorSelectName = 'Color';

      var sPriceSelectName = 'ColorPrice';

       

       

      // Get the select object with name

      var select = this.getField(sSelectName);

      var selectColor = this.getField(sColorSelectName);

      var selectPrice = this.getField(sPriceSelectName);

       

       

      // Clear items

      selectColor.clearItems();

      selectPrice.clearItems();

      select.clearItems();

       

       

       

       

      var iSelectNameLength = sSelectName.length;

      var aSelectOCGs = new Array();

       

       

      // Get all layers

      var aOCGs = this.getOCGs();

       

       

      // Init.

      var aShapes=new Array();

      var sOCGsName='';

      var sShapeName='';

      var aColors=new Array();

      var sColorName='';

      var iOCGsNameLength;

       

       

      // Insert the items

      var i;

      for (i=0; aOCGs && i<aOCGs.length;i++) {

                sOCGsName = aOCGs[i].name;

                sShapeName = sOCGsName.substring(0, iSelectNameLength+2);

                iOCGsNameLength = sOCGsName.length;

                sColorName = sOCGsName.substring(iSelectNameLength+3, iOCGsNameLength);

       

                if (sOCGsName.substring(0, iSelectNameLength) == 'Shape') {

       

                          // For colors

                          if(!inArray(sColorName, aColors)) {

                                    aColors.push(sColorName);

                                    selectColor.insertItemAt(sColorName);

                                    selectPrice.insertItemAt(sColorName);

                                    console.println(sColorName+' added');

                          }

       

                          // For shapes

                          if (!inArray(sShapeName, aShapes)) {

                                    aShapes.push(sShapeName);

                                    select.insertItemAt(sShapeName);

                                    console.println(sColorName+' added');

                          }

       

                }

      }

       

       

      }

       

      // Clear the console

      console.clear();

       

       

      // init

      init();

       

       

      // Change button

      var btn = this.getField('Change');

      btn.setAction("MouseUp", "refreshLayers()");

       

       

      console.println('end');

       

       

       

       

       

      Someone see something false?

       

      Thanks for help and sorry for my bad english.

        • 1. Re: Can't execute functions correctly in a button event
          try67 MVP & Adobe Community Professional

          This code does look OK... maybe the error comes from some other code in your file?

          1 person found this helpful
          • 2. Re: Can't execute functions correctly in a button event
            phoenix2ch Level 1

            Tanks for your answer try67,

            It's the only code, i checked with the button "Modify all javascript" (named in french for me "Modifier tous les scripts javascript")

            And there is just my code in the comments

            //<Document-Level>

            //<ACRO_source>test</ACRO_source>

            //<ACRO_script>

            /*********** appartient à : Document-Level:test ***********/

             

            [my code]

             

            //</ACRO_script>

            //</Document-Level>

             

             

            and the event for the buttons:

             

            //<AcroForm>

            //<ACRO_source>Change:Annot1:MouseUp:Action1</ACRO_source>

            //<ACRO_script>

            /*********** appartient à : AcroForm:Change:Annot1:MouseUp:Action1 ***********/

            refreshLayers();

            //</ACRO_script>

            //</AcroForm>

             

            I try to delete it and re-execute, but no change :/

             

            There is something strange, if a delete, for example a function, i always can call this function.

            So maybe my code is saved somewhere else, but i don't know where.

            • 3. Re: Can't execute functions correctly in a button event
              try67 MVP & Adobe Community Professional

              Very likely you have the same code appearing in more than one place. Do you

              have any folder-level scripts installed?

              If you wish, send me the file in question to try6767 at gmail dot com, and

              I'll have a look at it.

              1 person found this helpful
              • 4. Re: Can't execute functions correctly in a button event
                phoenix2ch Level 1

                If i put my event fonctions in an other file, it's work

                I don't understand what is exactly the probleme, maybe it's because I restart with a new PDF file.

                 

                Thanks you very much

                 

                ++

                • 5. Re: Can't execute functions correctly in a button event
                  linopasc

                  I had similar problems when editing document-level javascript (javascript pro 8). After changes the new script works as the old script. In all cases  I found a syntax error in the new script. Unfortunately the javascript editor does not give any message for syntax error. I use the following to reveal syntax errors:

                   

                  Suppose to write the following  document-level script

                   

                  app.alert("Ok")

                   

                  function Sum(x,y)

                  {

                  app.alert(x + y)

                  }

                   

                  when you close the editor you will recieve the message "Ok"

                   

                  suppose instead to write

                   

                  app.alert("Ok")

                   

                  function Sum(x,y       // (bracket missing)

                  {

                  app.alert(x + y)

                  }

                   

                  you will not recieve the message because the syntax error. Try it !