Forum Discussion
Exit SL via JavaScript
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
- OwenHoltSuper Hero
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. - SamClarkCommunity Member
I will give it a try... Thank you!
- TimMarshall-476Community Member
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
- OwenHoltSuper Hero
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.
- RobTaoCommunity Member
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.
- MathNotermans-9Community Member
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 :-)
- RobTaoCommunity Member
Thank you for the detail response. I'll try my best, but some of programming is beyond me at the moment.
Rob
- MathNotermans-9Community Member
Working on fixing it...either today i will get a working version....else sometime this week...
- MathNotermans-9Community Member
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.
- RobTaoCommunity Member
Really appreciate this. I followed as far as up to last part about the JQuery, then i was lost.
A coworker also suggest to include this at the end, not sure if thi s would help,
var dialog = ngDialog.close();
Thanks again!
Rob
- MathNotermans-9Community Member
Parts work...not all...when testing locally it nicely closes the project...online not yet.. have to go now.. but here is my test file.
- RobTaoCommunity Member
Hi Math, thank you so much for your help! I'll see if I can work it out with a coworker with your attached sample.
Rob
- MathNotermans-9Community Member
If you check my other posts you can find MouseEvents samples. Adding those and you have all the components needed.
Kind regards,Math
Related Content
- 5 months ago