Forum Discussion
Using Javascript to trigger sending an email
Hi Guys,
Long time lurker, first time poster here.
I am currently building an App Sim of a form that we use, so a lovely amount of variables etc - the current form on submission is sent to a database , sadly there is no training environment thus why I am here.
What I am trying to achieve is have the user input an email at the start of the form and once they have completed all the fields, they submit it - this pops an new email PULLING the recipient from the first field and then has all of the answers entered pulled to the body of the email using variables.
This is the Javascript I am currently using:
var player = GetPlayer();
// Get data from Storyline variables
var bodyContent = player.GetVar("Role_Output");
var subject = "Use of Force Form Completion"; // Can also be a variable
var to = "Recipient"; // Can also be a variable
// Encode the body to handle special characters and spaces
var mailto_link = 'mailto:' + to + '?subject=' + encodeURIComponent(subject) + '&body=' + encodeURIComponent(bodyContent);
// Open the mail client
window.open(mailto_link, 'emailWin');
This pulls the Subject and body fine, but in the TO field it just says 'Recipient'
Has anyone achieved this at all?
I don't understand Javascript to be fair but can kind of work it out based on the comments etc. in the above example.
Any ideas my fellow ID's?
Thank you in advance!
3 Replies
- AndrewBlemings-Community Member
The relevant JavaScript in the code snippet provided is:
var to = "Recipient"; // Can also be a variable
So your mailto_link is doing what it's being told to: giving the variable "to" the value of "Recipient." That "Recipient" needs to be replaced with a reference to the variable you're using to store the user's inputted email address.
If at the beginning of the course the learner's typed email is stored in a variable called learnerEmail, then your code should read
var to = getVar('learnerEmail');
- DanEccleston-c1Community Member
That has worked a treat.
Can I ask one more question?
In the body of the email, ideally I want the question on one line , then the answer selected underneath..
How would I do that?
So say the first question in the example above is : Confirm Role.. the answer uses variable 'Role_Output'
If I can get one line done , I can duplicate it for the rest!
Thanks so much!- AndrewBlemings-Community Member
I confess I'm less sure for your implementation. The easy answer is that new lines can be created in strings of text using the \n "new line escape character." So if I had a variable foo and I assigned it the value '123\n456', it should print out as
123
456
I'm much less familiar with your encodeURIComponent function, but I expect your "var bodyContent =" assignment can fit in a new line escape between the two strings of text.
var bodyContent = getVar('Role_Question') + '\n' + getVar('Role_Output');
Related Content
- 1 year ago