Exit SL via JavaScript

Jun 25, 2019

I'd like to write a script that alerts the learner and exits the course after learner acknowledgment.

I could do a window.close() but some LMS encapsulate the course so using the built-in SL EXIT trigger would be better. 

Is there a tried-and-true way to activate a trigger or otherwise invoke the SL EXIT from JavaScript?

17 Replies
OWEN HOLT

Are you talking about a typical web page alert box?

In SL, set a variable called Exit_Course with a default value of "False".

Execute the following JS based on your triggering action.

let player = GetPlayer();

function myFunction() {
     if (confirm("You are about to exit the course!")) {
     player.SetVar("Exit_Course",true);
    } else {
    console.log("User Cancelled Exit!");
    }
};
myFunction();

In SL, add an action to EXIT COURSE when the VARIABLE Exit_Course changes IF Exit_Course is equal to True.

So basically, the JS controls the variable and the variable controls the exit function in SL.

Tim Marshall

Hi All,

I have attempted the solution by Owen (as shown above), though this does not seem to work for me on an ipad or Safari. I have published content as web output hosted on AWS with a link that exits for most other browsers and devices, though not on ipad.

Has anyone found a solution for this?

I'm not blessed with technical ability, so any help would be much appreciated. 

Regards

Tim

OWEN HOLT

The request was for JavaScript which can operate a little differently from browser to browser. I love JavaScript. It can do some amazing things. But it isn't always the right tool for the job. For something like this, I would keep it simple by simply triggering a layer with your Exit message and buttons to either close the course or dismiss the warning and return to the course (close the layer).  Sometimes, the simple solution is to use the built-in functionality.

Rob Tao

Hi Owen or anyone who would like to give me a second eye,

I'm not a programmer and I had help on this. But I'm trying to setup and automatic exit course, after a user doesn't do anything for a time. I've set this to 2 mins just for the test. 

The publish course would be in TinCan, but the exit course isn't executing. I'm not sure what might be wrong with the logical order of the execute is. 

FYI, the JavaScript is in the master.  I thought this would be the most ideal place, instead of placing at the beginning of each page. This is also where I've included the variable as example provided by Owen above. 

Any help is greatly appreciated, thank you, Rob.

Math Notermans

One obvious error i noticed in your Javascript... <script> tags were in the code. They are not needed when using triggers in Storyline to execute JS. After removing that and checking the console i noticed an error that 'player' was called multiple times. That indicates your setting that variable at multiple positions whereas that is only needed once. Im gonna check it further...

Removing the let player variable i got rid of the error, but it still doesnot work. I do suspect your timer function is never called. What i often do to get things like this working...add console.log statements to your code to see if something happens...and debug that way. Also helps to see onscreen the needed variables.. 'Exit_Course' being one of them...gonna check some more...

 

Ah yes.. another never executing part of code... $ signs in code indicate jQuery is used. And since quite a while jQuery is not standard included in Storyline. So you either need to manually add jQuery or use Vanilla Javascript for all parts with $ in the code.

Quite sure this is the final barrier...
This is a good resource for converting jQuery to Vanilla JS
https://tobiasahlin.com/blog/move-from-jquery-to-vanilla-javascript/

And still some errors.. 'startClock' is not defined.  Gonna get it right :-)

Math Notermans

What you basically should do with this kind of projects... break it up in small blocks..get those working and then combine them.

Blocks would be:

1) Properly working timer. Being able to set and reset it.
2) Mouseevents. Make sure you can track any mouseevents
3) Exit course through Javascript

If you have these 3 blocks working in simple separate Storyline's. Try combining them... and only then try to do the same in the MasterSlides.

Rob Tao

Hi Math, 

I'm hoping to pick your brain on another sample I have. Unfortunately the sample you've sent is completely revised now. Me and a programmer manage to apply a sample work around. The file attached is a stripped down version for the client.

The only pages that wouldn't automatically exit the course, after a time out, were multi layers slides. in fact, the invisible, to allow the user to continue the course also wouldn't show up. 

If anyone else would like to help, would be great appreciated.

Thank you, Rob

Math Notermans

Although not perfect this one, my previous approach with a Javascript Timer does work even with layers. In fact the exit is triggered on a Slide Layer itself.

What might be missing from your sample is a timer. And i did notice some choices on Slide Layer behaviour that might cause it not to work.

However this sample works and closes the SL after a minute automatically... on any slide on any layer....

Kind regards,
Math