Retain line breaks in text variable when exporting via Javascript
Apr 04, 2013
My goal is to have participants create three different Action Plans during the course, then at the end of the course display all three action plans side by side and give participants an option to email the action plans to themselves and cc: their manager. After much wrangling, I've figured out the variables and javascript to make all this happen exactly how I want except for one tiny detail.
When the participants complete each action plans, I would like them to write them in a list format, like so:
first action
second action
third action
When the action plan is displayed at the end of the course, it retains the line breaks properly and displays it exactly as the participant typed it in. However, when I use javascript to grab the variable for the action plan and place it in the body of the email using mailto:, it displays like so:
first actionsecond actionthird action
Is there any way to preserve the line breaks from the text entry box when it's exported via javascript?
Thanks!
Here's the script I'm using and I'll attach the story file I've been testing with.
var player=GetPlayer();
var name=player.GetVar("UserName");
var topic_one=player.GetVar("TopicOneActions");
var topic_two=player.GetVar("TopicTwoActions");
var topic_three=player.GetVar("TopicThreeActions");
var email=player.GetVar("UserEmail");
var cc=player.GetVar("ManagerEmail");
var subject="Action Plan for "+name;
var body="Topic One: "+topic_one+"%0D%0ATopic Two: "+topic_two+"%0D%0ATopic Three: "+topic_three;
var link='mailto:'+email+
'?subject='+subject+
'&cc='+cc+
'&body='+body;
window.open(link);
14 Replies
Hi Tracy
You need to replace all the hidden new line characters, so after this line
var body="Topic One: "+topic_one+"%0D%0ATopic Two: "+topic_two+"%0D%0ATopic Three: "+topic_three;
INSERT
body = body.replace(/\r\n?/g, '%0D%0A');
This will search for all new lines \r\n and replace them with the correct characters
Updated file attached
Doug
Thank you Doug, that did the trick! Much appreciated!
Doug,
After three grueling days of scouring for answers, I finally found yours! I was able to customize for my project and it works like a charm!
Many, many thanks. Sending good Karma your way.
Glad this thread helped you out Tonya!
Hi folks...
I am attempting to do the exact same thing here...
Im using Doug's script to capture some notes that the learners types in as they proceed through the course....
I downloaded Doug's .story file , published it as SCORM 1.2 and ran it locally. it worked perfectly. When I clicked submit, my Outlook client launched and the email fields were all filled in as designed..
I used that same script (just swapped out for my variable names) and published for SCORM, and the Outlook Client will not launch. So confused!!!!!
OOps. I meant to send this along...here is my modified code. I took out CC line as I hadnt a need for that..
Basically, Unit1Notes, Unit2Notes, and Unit3Notes are text entry fields that allow the learner to enter in notes as they proceed through course... learnermail variable is the learner's email that they entered earlier in the course...
var player=GetPlayer();
var notes1=player.GetVar("Unit1Notes");
//notes1 = notes1.replace(/\r\n?/g, '%0D%0A');
var notes2=player.GetVar("Unit2Notes");
var notes3=player.GetVar("Unit3Notes");
var email=player.GetVar("learneremail");
var subject="Course Notes for "+learneremail;
var body="Unit 1 Notes: "+notes1+"%0D%0AUnit 2 Notes: "+notes2+"%0D%0AUnit 3 Notes: "+notes3;
//NEW LINE
body = body.replace(/\r\n?/g, '%0D%0A');
var link='mailto:'+email+
'?subject='+subject+
'&body='+body;
window.open(link);
and heres the .story file .
If anyone can figure out why its not opening a mail client window let me know! thanks!
Hi
I'm working on a project which has a notepad - the users can add text to it as they go through a module. I've used this: body = body.replace(/\r\n?/g, '%0D%0A'); to keep the line breaks intact when sending the text from the notepad to email.
Does anyone know if there's any equivalent that works when printing?
Thanks
Andy
Hi Andy,
This thread is a bit older so I'm not certain if folks are still subscribed, but you may want to reach out to them using the "contact me" feature on their profiles.
Thanks Ashley
Andy
This post was removed by the author
body = body.replace(/\r\n?/g, '</br>');
That should work for printing I think if the variable is being showing in HTML to print.
Thanks for sharing your suggestion, Jackson. :)
Hello,
I looked at Doug Brown's storyline file, javascript_2btest_2b-_2bgeneric_doug.story, to test and adapt for a project I'm working on. I noticed that the published file works perfectly in IE but not Firefox. I'm hoping that others in the community may have a solution for this problem.
Thank you!
Hi Daniel,
Are you testing the published output by running it locally in IE? You'll want to test it out by uploading to the intended publish environment as described here.
This discussion is closed. You can start a new discussion or contact Articulate Support.