Expand my Community achievements bar.

Insert Script Object?

Avatar

Level 2
In Acrobat Pro you can create a document script as follows:



function Hello()

{

app.alert("Hello World")

}



And call this Hello.



If you create button on the page then create an action on Mouse Up Run a JavaScript which looks like this:



Hello();



A message box appears saying Hello World click OK and press the button and it keeps coming back.



Now in Designer this is a little different.



I create a new page, insert a script object rename it to Hello and type:



function Hello()

{

app.alert("Hello World")

}



I then create a button and on the mouse up action type;



Hello();



If I try this I get the following mesaage:



TypeError: Hello is not a function

1:XFA:form1[0]:#subform[0]:Button1[0]:mouseUp



Why is it so?



Even removing the script object and going to the Form properties Variables tab and entering the function script there does not seem to work.
6 Replies

Avatar

Level 2
Why is it that after you post a message you go back to the problem and play a little bit more and work out the answer?



I change the script object slightly so as to not confuse myself but still called it Hello:



function Welcome()

{

app.alert("Hello World")

}



Then on the action mouse up I used:



form1.variables.hello.Welcome();



And what do you know it all works.

Avatar

Level 2
Back to square one.



Using the logic above if I make a script object and call it setTotals with the following script in place:



function Totals(){

var totalval = form1.costs.spei1.rawValue + form1.costs.spei2.rawValue;

if (totalval == 0 || totalval == null || totalval == "")

{

this.rawValue = "";

}

else

{

this.rawValue = totalval;

}}



Then in the field that is to display the totals I put the following script under calculate:



form1.variables.setTotals.Totals();



When I type a figure into spei1 or spei2 nothing happens.



Am I missing something?

Avatar

Former Community Member
Hi,

If you're using the
this pointer within a scripting object, it points to the scripting object itself, not to a form variable!



Try this one:




function Totals(obj){

    var totalval = form1.costs.spei1.rawValue +     form1.costs.spei2.rawValue;

    

    if (totalval == 0 || totalval == null || totalval == "")

        obj.rawValue = "";

    else

        obj.rawValue = totalval;

}





...then call this function using:




form1.variables.setTotals.Totals(this);





Here you go. :-)



Regards,

Steve

Avatar

Level 2
Thanks Steve,



It works without a hitch. Im still getting my head around JavaScript and 2 weeks, 6 hours a day of reading and testing and reading is slowly getting clearer I think.



Just to make sure Im getting this clear in my head. The way I had the script before was when it was applied to the field that showed the value it was referring to this as the field itself. So it worked not worries because it was point to the itself.



When I tried to apply the same script in a script object field (variables) that the whole documents can reference it was only in as a variable. The line form1.variables.setTotals.Totals(); did not do anything because it did not see the totals as a function and refer to it, and as a result nothing would appear in the filed. By adding obj to the script it tells the form that its an object and the objects formula is spei1 + spei2. This now means that the script can be applied to any filed and that is where form1.variables.setTotals.Totals(this); comes in. It tells the filed apply the object (Totals [spei1 + spei2] ) to this filed.



Does this mean that any script placed in the Variables field needs to be referred to as objects?



The reason I was taking this approach was to resolve a problem posted earlier.






I thought by having the script as a script object that was applied to the document rather than the f...




















Avatar

Former Community Member
Hello, I tried to undestand your messages - but still have a general problem in understanding the issue.



I would like to define the following function which I want to use on 20 textfields. But I don´t know where to place the definition - and how to define the path on the exit-event in each textfield.



function fertig ()

{

if (this.rawValue != null)

{

this.ui.oneOfChild.border.presence = "hidden";

} else {

this.ui.oneOfChild.border.presence = "visible";

}}

Can you help with this?

Stefanie

Avatar

Former Community Member
Stefanie,



What do you want to accomplish? Do you want to use the method
fertig with the exit-event of multiple form fields?



Steve