Calling JavaScript Functions

I suspect I am missing the right procedure or syntax for calling functions in AP9.

I created a function and placed it as an onLoad Action to the slide. After writing the function - I then called it using typical syntax newProblem();

It worked great and rendered on the screen as expected.

However - I then created a button and wanted to call the function each time the button was clicked. I created an Execute JavaScript Action for the button and simply tried to call the function again using typical syntax newProblem();

It did not work. - I also tried to put the function on a Project Load Action - hoping it would always be available to call. I had hoped it would be at least available on the same slide.

Do I need to call it a different way? Place the function somewhere else?

I placed the entire function and called it on the button the same as I did on the slide enter and it worked just fine but that seems to defeat the purpose of making a function in the first place. I should only have to make it once and then call it as I need to.

What part am I missing to get them working in both places with one function?
Thanks in advance.

Hi Greg,

When a function is defined within an event/action, it only has a local scope within that context. Other actions don’t know of its existence and cannot call it.

To create a global function that can be called in other events/actions, you need to define it like this:
For example, let’s consider a function named newProblem:

window.newProblem = function() {
    // function body
};

Once you have defined it, you can call this function as usual:
newProblem()

Regards,

1 Like

Thank you, @Hang :+1:t3:
That worked great!

1 Like

For what it is worth, I just downloaded the trial version yesterday. I still have much to investigate but the ability to use JavaScript in this way is a huge need. So far - I am very pleased with everything I am seeing - including the support through this forum.

Why aren’t more people talking about this tool?

4 Likes

I always questioned the same. Why don’t many people out there talk about this tool?

2 Likes