Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

Function call from button works then vanishes if I click anywhere

Avatar

Former Community Member
I have the following code and it works great some times but others nothing happens, the button seems to work then for no apparent reason (at least nothing that comes up in the javascript console) I click and it says "FundName.FundTitle.getFundTitle is not a function" but it worked several times before that and nothing changed. Feel like I'm losing my mind, please give me some insight if you have any, everything is set to export for acrobat 9 and I have reader nine installed.



DeedEstablishmentForm.Page1.Button2::click - (JavaScript, client)

var str = FundName.FundTitle.getFundTitle();



I have a fragment that has the following:

form1.FundName.FundTitle::initialize - (JavaScript, client)

function getFundTitle(){

this.str = new String(this.rawValue);

this.start = str.length - 20;

this.idx = str.toLowerCase().lastIndexOf('superannuation fund');

xfa.host.messageBox(this.str.valueOf());

xfa.host.messageBox(this.start.toString());

xfa.host.messageBox(this.idx.toString());

if(this.start >= 1 && this.idx >= this.start && this.idx >= 1){

return this.str.slice(0,idx);

}else{

return this.str;

}

}
2 Replies

Avatar

Former Community Member
Can you post your form to livecycle8@gamil.com and I will try to duplicate. If I can duplicate it then I will try to see what is going wrong.

Avatar

Former Community Member
I talked to the developers about this one and I think we figured out what is happening. It has to do with the scope of the function. Here is what we believe is happening. You have defined your function on the Initialize event of an object. The function gets created and at the end of the Initialize event script it is no longer needed so it is marked for deletion. The function is still around until the next garbage collection cycle. Now the form finishes loading and you hit the button to call the function. Because th efunction still exists it is called. You continue to do this and all is OK. At some point garbage collection will run and the function will be deleted. It is at that point that you call it again and get the error. To resolve this problem you need to ensure that the funstion is always available. If you move your function to a script object then when the form loads the script object will run and your function will be created. Now it is callable at all times and shoudl alleviate th eissue you are having.



On another note I noticed the use of the "this" key word in your script. In our world the "this" object is the obejct that has focus (or who is making the call). The ability to extend the "this" object as you do with str, start and idx is going to disappear. The "this" object will become immutable in a future release. To ensure that you do not have issues in a future release I suggest you change the way you do that code.



Lastly by moving the code to a script object you will not be able to use "this". You can pass an object to the script and use that instead.



Hope that helps. Let me know if you need further explanation.



Paul