Forum Discussion
Javascript question
Hi - I am trying to write a javascript that allows learners to email notes from a Storyline course. I found a post with an example of a similar project, and tried copying that script, but it didn't work. I also found an article about recent updates to javascript support. It had an email script which I tried. I'm sure I'm missing something small, but just can't figure it out! I've attached the file I'm working on. If someone can give some advice, I'd appreciate it!
- ScottWiley1Community Member
I couldn't open your file as it's using advanced custom text styles, or a more current version.
I could take a look at your code if you paste it here. Or if possible to save to earlier version I can open.
- MariaCSStaff
Hi, Shelly.
Thank you for sharing your .story file!
I looked at your project, and while I'm not super familiar with Javascript, I noticed a few lines missing from your code. The project seems to work now after the changes below:
var player = GetPlayer();
var email = player.GetVar("TextEntry");
var subject="Full Item Spec Sheet";
var thoughts1 = player.GetVar("Thoughts1");
var thoughts2 = player.GetVar("Thoughts2");
var thoughts3 = player.GetVar("Thoughts3");
var body_start = thoughts1+' '+ thoughts2+' '+thoughts3;
var mailto_link = 'mailto:' + email + '?subject=' + subject+'&body='+body_start;
win=window.open(mailto_link,'emailWin');
I hope this helps!
- ShellyBlair-3b0Community Member
Thanks so much! I knew I was missing something small, but couldn't figure it out. I really appreciate your help.
- MathNotermans-9Community Member
As Shelly already mentions you are making some basic mistakes. Do use the console in the browser for clues on what is going wrong. You donot need to be a experienced programmer to use basic Javascript.
First very basic mistake you made, not related to Javascript, but to keeping your projects in Storyline clean and workable, is using the default 'Text Entry' naming of Storyline as your variable names. Do not do that. Every time you create a variable give it a recognizable custom name.
So here you see your code ( on the left ) compared with my code ( on the right )
'let' is just a modern version of 'var'.
When you compare it you will see quite some differences. First striking one is the Text Entry variable. If you run your version you will notice Storyline tells you it cant find TextEntry. Well there you have the first typo ;-).
Continuing you notice that you dont set variables when you get Thoughts from the Player...
so how do you plan to pass those to the email-script ?
And body_start is nowhere defined and set..
Plainly copying and pasting Javascript snippets you find, will in the end always give issues.
Do try to understand whats happening and why. Start using the console to debug your code.
But if you like the possibilities Javascript gives you in Storyline...the sky is the limit.
Here is your Storyline fixed and working.
Kind regards,
Math
PS. Somehow your Storyline is incredibly large 160 MB + and i thus cannot add it to this post.
So adding the code as its working. Do hope you can get it working..let player = GetPlayer();
let email=player.GetVar("emailAdress");
let subject="Full Item Spec Sheet";
let th1 = player.GetVar("Thoughts1");
let th2 = player.GetVar("Thoughts2");
let th3 = player.GetVar("Thoughts3");
let body_start = "You filled in "+th1+"and "+th2+". Finally you added "+th3+". Thanks for using our services.";
var mailto_link='mailto:'+email+'?subject='+subject+'&body='+body_start;
win=window.open(mailto_link,'emailWin');