Javascript in Storyline 360

Oct 19, 2021

I need to create a trigger that - when the learner clicks the Exit button - will  automatically create and send an email that contains free-text survey results. There are two variables that need to be included: the learner's name (UserName), and the free-type entry (CalculatorText). This is the code I added:

var player = GetPLayer();
var email = "email@company.com";
var user = player.GetVar("UserName") ;
var text = player.GetVar("CalculatorText") ;
var subject = "Calculator quiz response" ;
var emailBody = "The response for\n" + user + "is:\n" + text;
var mailto_link = 'mailto: '+ email + '?subject=' + subject + '&body=' + encodeURIComponent (emailBody) ;
win = window.open(mailto_link, 'emailWin') ;

I've tested it in Review 360 and the Articulate Tempshare site, with no success. Any suggestions would be most welcome.

6 Replies
Walt Hamilton

Depending on your browser, %0D%0A may work better than \n

Not sure what "no success" looks like, compared to what "success would look like, so I'm imagining that you mean nothing is happening. If that's the case, use the following  Javascript debugging tool:  alert ("message");

Place this anyplace in your js. If you see a message box with "message", the error is after the alert line. When js encounters an error, it stops at that point. Continually moving the Alert down through the script will allow you to isolate the line where execution fails.

One problem SL is known for is changing straight quotes and curly quotes. I don't know if that's a problem here, but dropping the script into Notepad, and copying it to the js pane, instead of entering it from the keyboard will cure that problem, if that's what it is.

Math Notermans

A few fixes to get this working. See the sample on Review...
https://360.articulate.com/review/content/d3c5d2eb-60ea-4474-914e-0cff915b0232/review

You need to be aware though that you have a proper default mail-client set on your computer...as that is what will be used. With Javascript you cannot test whether a user has a default mail client installed. If a user has not, 'mailto:' always fails.

This code works.
var player = GetPlayer();
var email = "email@company.com";
var user = player.GetVar("UserName") ;
var text = player.GetVar("CalculatorText") ;
var subject = "Calculator quiz response" ;
console.log("mailto: "+user+" | "+text)
var emailBody = "The response for "+escape('\r\n') + user + " is: "+escape('\r\n')+"text";
var mailto_link = 'mailto: '+email+'?subject='+subject+'&body='+emailBody;
win = window.open(mailto_link, 'emailWin') ;

Notice the differences with your code...

GetPlayer instead of GetPLayer.
Javascript is picky about capitals.

The emailbody line uses escape('\r\n') and that part of code isnot included in a string... its outside the string. No need for encodeURIComponent as escape already does that.

And here you have a complete working sample. Do use a external editor like Sublime Text or Brackets to edit your code. The Storyline ide doesnot show errors and really is no good. The tools mentioned all have colorcoding, hints and code checking. Makes life so much easier.

Kind regards,
Math

Cindy Bartrop

Thanks - appreciate the help!

Cheers,
Cindy Bartrop
Instructional Designer/
Documentation Lead

rVox: 121-5608
direct: +1.778.331.5608
mobile: +1.604.315.6529
email: cbartrop@rbauction.com

9500 Glenlyon Parkway
Burnaby, BC, CA V5J 0C6

[cid:image001.png@01D7C5CD.AAA9A0D0]

Our Safety Commitment:
to send everyone home, every day,
the way they came to work

[Macintosh HD:Users:seabay:Desktop:f-ogo

Cindy Bartrop

Thanks so much - once I removed the quotation marks around "text", it worked perfectly!

Cheers,
Cindy Bartrop
Instructional Designer/
Documentation Lead

rVox: 121-5608
direct: +1.778.331.5608
mobile: +1.604.315.6529
email: cbartrop@rbauction.com

9500 Glenlyon Parkway
Burnaby, BC, CA V5J 0C6

[cid:image001.png@01D7C5CD.CCD35B10]

Our Safety Commitment:
to send everyone home, every day,
the way they came to work

[Macintosh HD:Users:seabay:Desktop:f-ogo