Beginner JavaScript Guide

This is meant to be the first of a three part project.

What I have here is an interactive look at implementing some JavaScript.
The guide provides some basic actions that you can interact with and shows you the code that makes them work. This is meant to be a beginner guide and the other two projects will be an intermediate and an advanced guide that help demonstrate more challenging JavaScript. If anyone notices bugs - please let me know so that I can correct it.

Beginner JavaScript Guide

Hopefully this resource will help someone out.

11 Likes

Thank you very much for this very well done tutorial.

I only have one question: Can the “prez.object …” objects be called and controlled from anywhere?

For example, can I use it to control a Slide 5 object from Slide 3? Or do I need to specify a more precise target? Or are all objects global?

Thanks for your help

1 Like

The use of JavaScript and prez.object as shown in the project will not control objects on another slide.

If there is a way to address an object on another slide - I have not discovered it.

You might consider accomplishing this task with variables.
If the learner performs an action on slide 3 that should affect an object on slide 5, you might set the variable on slide 3 and then check for that variable onEnter of slide 5 and have it display accordingly.

1 Like

Hi,

I just wanted to add some information about this.

HTML5 only creates objects for the current slide, so there is no and cannot get objects from other slides.
If you need to set something on different slides, you can do so through variables or by adding properties to the prez object, then set them in the On Load event of that slide as Greg said.

Regards,

1 Like

Thank you very much for your answers.

If I have understood correctly, the variables used for this must be global variables so that they are available everywhere.

Unfortunately, I have not yet found how to define global variables in the manual.

Or perhaps all variables are automatically global?

If you create the variable through the UI - you should be able to work with it from any slide.
It will also allow you to display them on the slide using %myVar%

image

1 Like

One last question regarding the variables:

If I use JavaScript functions, how can I access the variables (created via the UI)? And how can I use variables created via JavaScript within AP?

Example: If I create a random value via JavaScript. How can I then pass this on to an internal variable?

Or, if I store in a variable how often the user has given an incorrect answer. How can I then use this variable in JavaScript?

Thank you for your patience - and as I said, this is the last question on this topic. :wink:

If you wish to simply query the current value of the variable you can do the following.

prez.variable("variableName");

Where variableName represents the name of the variable you wish to query.

To assign the random number to your variable you can do something similar to this.

prez.variable("myVar",Math.floor(
  Math.random()*11
  ));

This would set the myVar variable to a random value between 0 and 10

To compare your variable to another value - you can do something like this…

if (prez.variable("wrongGuesses") == 3) {
   code to execute here;
}

Hope that helps.

1 Like

Thank you very much.

You’ve been a big help to me :+1:

I will try out what I have learned over the next few days. :smiling_face: