18 Replies Latest reply on May 12, 2016 8:08 AM by alexh90310518

    Functions not defined in a spawned page

    alexh90310518 Level 1

      Hello,

      In my document I have multiple functions defined at the document level, these are then called on hidden templates which get copies spawned into the document, they used to work fine but all of a sudden they have all stopped working and when the de-bugger console is opened it states that 'functionName' is not defined.

      Edit: It's not just spawned pages, it's all my functions across all pages.

       

      The only changes I have made is that my Functions document level script area was getting a bit full so I created a new one called Functions2, however even when I combine these two and delete the new, now empty, document level script the functions still do not work when not too many changes back they did.

       

      I don't know what I've done or how to fix what has happened.

      Any help is much appreciated.

      Thank you in advance!

        • 1. Re: Functions not defined in a spawned page
          Bernd Alheit Adobe Community Professional & MVP

          Get you any error message when you close and re-open the document?

          • 2. Re: Functions not defined in a spawned page
            alexh90310518 Level 1

            Yes ReferenceError: 'functionName' is not defined 12:Field:MouseUp

            • 3. Re: Functions not defined in a spawned page
              Bernd Alheit Adobe Community Professional & MVP

              Can you share a link to the document?

              • 4. Re: Functions not defined in a spawned page
                alexh90310518 Level 1

                I can indeed.

                 

                Here's a link to the dropbox that it is in.

                Dropbox - VS Form

                 

                To get the error message you will want to click the orange button labelled "Glazing/Frame Extras"

                The functions as I said are defined in the Document Level 'Functions'

                • 5. Re: Functions not defined in a spawned page
                  Bernd Alheit Adobe Community Professional & MVP

                  When I open the document I get following error:

                   

                  ReferenceError: prefix is not defined

                  41:Document-Level:Arrays

                  1 person found this helpful
                  • 6. Re: Functions not defined in a spawned page
                    alexh90310518 Level 1

                    Ah yes that's supposed to happen at the moment, prefix is defined for the p#.'templateName'.'fieldName' that happens when a new page is spawned in, it's been like that for quite a while I just haven't got around to solving defining it globally yet.

                    My functions that didn't use those arrays still functioned before this happened however.

                    • 7. Re: Functions not defined in a spawned page
                      Bernd Alheit Adobe Community Professional & MVP

                      The Javascript interpreter will stop at line 41 and don't read the following functions definitions.

                       

                      When you define the variable prefix all functions will be defined also.

                      1 person found this helpful
                      • 8. Re: Functions not defined in a spawned page
                        alexh90310518 Level 1

                        Any thoughts on how to define this globally?

                        var fnarr = event.target.name.split(".");

                        var prefix = fnarr[0] + "." + fnarr[1] + ".";

                         

                        That is how the 'prefix' part works.

                         

                        Edit: I figured it, just remove the 'var' part.

                        Thank you very much for your help Bernd Alheit!

                        • 9. Re: Functions not defined in a spawned page
                          alexh90310518 Level 1

                          Okay so it all works inside Acrobat but it doesn't work inside Reader, is there a way I can access the de-bugger console or see what is happening differently inside Reader to Acrobat?

                           

                          Dropbox - VS Form

                          Working version in Acrobat (Doesn't work in reader).

                          Glazing/Frame Extras button should spawn page and advance focus to the next page, + - buttons under Add-ons should increase and decrease values accordingly, they don't. Finish button should set the window preview, delete spawned page and advance focus to previous page.

                          • 10. Re: Functions not defined in a spawned page
                            Karl Heinz Kremer Adobe Community Professional

                            You can add this one line to your document level script (do this as the first line, so that any potential errors will be shown):

                             

                            console.show();
                            


                            You can also open up Reader's preferences, then select the "JavaScript" category and check the "show console on errors and messages" setting.

                            1 person found this helpful
                            • 11. Re: Functions not defined in a spawned page
                              alexh90310518 Level 1

                              Thank you Karl, it is still telling me in Reader that the event.target.name and functions are not defined and yet everything is completely Ok in Acrobat.

                              I know Reader cannot handle some of the things that Acrobat can but I don't know what or even if I have done that Reader cannot handle in this situation.

                               

                              Error message;

                              (on open)

                              TypeError: event.target.name is undefined

                              2:Document-Level:Arrays

                              (on call of function)

                              ReferenceError: AddGlazing is not defined

                              12:Field:Mouse Up

                              • 12. Re: Functions not defined in a spawned page
                                Bernd Alheit Adobe Community Professional & MVP

                                event.target.name is not defined when you open a document. What name will you use here?

                                • 13. Re: Functions not defined in a spawned page
                                  Test Screen Name Most Valuable Participant

                                  I haven't looked at it but it SOUNDS as if you are mixing up what belongs inside functions with top level code. Top level code is executed only once, only on open. Certainly there should be no reference to "event.target" at the top level. Think closely about what you want to be executed when. A line cannot be moved to the top level if you need it to do different things each time; that belongs in a function.

                                  • 14. Re: Functions not defined in a spawned page
                                    alexh90310518 Level 1

                                    Well to be honest I'm not sure, I don't really understand what event.target really does, every time I've looked for an explanation as to what it is exactly I've been unable to find a clear explanation, just more examples of it being used.

                                    That top string of code;

                                    var fnarr = event.target.name.split(".");

                                    var prefix = fnarr[0] + "." + fnarr[1] + ".";

                                    Was given to me when I asked how to get spawned copies of hidden templates to work with each other, all I understand about it is that, that block must be executed otherwise the spawned pages wont work due to the fields having the prefix p#.'templateName'. and the bRename must be set to true or the whole premise of what I'm trying to do fails.

                                     

                                    At the end of the day I'm an utter beginner grasping at straws when it comes to JavaScript. As it stands I have no idea what I would put in place of event.target.name

                                    • 15. Re: Functions not defined in a spawned page
                                      alexh90310518 Level 1

                                      Okay that makes sense, I suppose I could try placing it in a function and placing that function to the top of the Document Level then calling that function first in the button that was clicked?

                                      • 16. Re: Functions not defined in a spawned page
                                        Test Screen Name Most Valuable Participant

                                        Hmm, I don't know what that is supposed to do either. But I understand what it does. (That is, I think it does something and ANOTHER bit of the code picks what you did).

                                         

                                        I can sort of explain what it does.

                                         

                                        In JavaScript, there are lots of events. Someone clicks on a button, that's an event. Someone changes the page they are looking at, that's an event. And there is an object called "event". Each time an event happens, some of your code might be run. And before it's run, the "event" object is prepared with a bunch of properties. Take a look under "event" in the JavaScript API. There's more detail there than you will ever want, but you can get a flavour of it. And look under event properties so see a list of what the properties are.

                                         

                                        event.target is an interesting one. It's the "object that triggered the event". So for a button click, this is the Field object for the button. But what about events like document open? In this case event is the document (Doc object). So, in a button click you might see event.target.textFont, which is the font on the button. But in a document open, event.target.textFont would just be wrong, because event is a Doc object not a Field object. Instead you might see event.target.creationDate, the document's creation date.

                                         

                                        So, knowing this, let's look at that bit of code: var fnarr = event.target.name.split(".");

                                        We're referring to the property name. So, if it's a Field this will be the same as saying

                                        var myField = event.target ;

                                        var fieldname = myField.name ;

                                        var fnarr = fieldname.split(".") ;

                                        This will do something with the field name, and give you a different result for each field. Perhaps what you need.

                                         

                                        What happens in doc open? We don't get far. Checking the API Reference, Doc has no name property.

                                         

                                        So, without knowing anything about what this is supposed to do, I'm quite sure you aren't supposed to use it at doc open time. I'd guess it is supposed to be in the JavaScript used for a field related event, so just put it there.

                                        1 person found this helpful
                                        • 17. Re: Functions not defined in a spawned page
                                          Bernd Alheit Adobe Community Professional & MVP

                                          The Acrobat Javascript Reference describes the event model.

                                          • 18. Re: Functions not defined in a spawned page
                                            alexh90310518 Level 1

                                            I think I understand what it does now.

                                            I think it takes the name of the field that was clicked and splits it up into segments so p# 'templateName' 'fieldName' then I think fnarr stores those into an array or something along those lines and defines that as 'prefix' so then when 'prefix +' is placed infront of a section of code, eg. this.getField("fieldName").value; to this.getField(prefix + "fieldName).value, it will still grab the field even though the name could be p1.template.fieldName or p99.template.fieldName.

                                            The problem is then is that there is an array of objects at the very beginning of the document (this is because there are about 50 - 60 items in the array) and I don't want to have to type out that array everytime I want to do something with those items.

                                            So I think the problem lies in the fact that I need to run that segment of code before the array defines it's self as the array is full of prefix + "Fieldname" objects as these are objects on a spawned page that are hidden/visible depending on conditions earlier in the document (basically if the user has selected for 2 windows it unhides all the secondary glazing options when the page is spawned, if only one window, hides them all).

                                            I suppose I could move the array(s) into the function that calls for them?