Skip to main content
Known Participant
July 27, 2022
Answered

Appending a new line to a Captivate variable in Javascript

  • July 27, 2022
  • 1 reply
  • 3198 views

I have a responsive project that uses a text entry box for the user to make notes whilst progressing through the project. What I would like to do is append some text to this depending on the outcome of various interactions.

 

I am using Javascript for one interaction and can update the variable used by the TEB without an issue. However, I am having problems adding a new line from within Javascript. I have searched for previous discussions here and online. I have tried adding the following line in the Javascript to update the TEB variable using the escape sequence "\n" for a new line, as specified in the MDN docs:

 

v_teb_notes=v_teb_notes  + "\nMy heading\nMy text to add.";

 

I believe that "\n" should add the new line, but when I run this the code fails. In the browser console I see the error "SyntaxError: '' string literal contains an unescaped line break".

 

When I remove the "\n" the JS executes, but no line breaks are added.

 

v_teb_notes=v_teb_notes  + "My heading My text to add.";

 

I am not sure what I am doing wrong. I have tried various options based on other threads here without success, including using the Scrolling Text Widget. I wonder if Captivate is doing something odd to the JS. This post from over ten years ago says that Captivate strips "\n" in Advanced Actions. I hoped this would not happen from JS, but since the JS is executed as an action maybe it does. This would explain the browser error. If so, is there a work around?

 

On a similar note, I cannot find a way to add a new line to a Captivate variable either. One thread recommended using HTML e.g. adding "<br/>" to the variable value, but that doesn't work.

 

Hopefully someone can help!

    This topic has been closed for replies.
    Correct answer OH_CP_Lover_&_Hacker

    I sent you a message to your profile here on this system..Hope it helps ya! 


    Ok, I would suggest putting your command that you are wanting to have update your desired TEB into a function. In the demo I posted, I'm loading an external Javascript file that has functions I made quicky to track and then update the last TEB.

    I just did a quick test...and just having the button execute the command you shared above will not work...but if you create a function (which is how I update TEBs) it will work for ya!

     

    So on the first slide I issue the following command to load my javascript file:

    $('body').append('<script src="assets/Supporting_Files/workfile.js" type="text/javascript"> </script>');
    });

     

    For quick testing and etc. I modified my applications Adobe Captivate 2019->HTML ->assets folder by adding a new folder called Supporting_Files....and in that folder I placed my "workfile.js" (as seen in the loading line above) so that when I go to preview the project it will load the needed JS file automaticly so things will work without having to play the folder add/shuffle/publish to weberver routine for each test run...

     

     

    1 reply

    OH_CP_Lover_&_Hacker
    Inspiring
    July 27, 2022

    Over the years I've used any of the following to add new lines into a Captivate string using JS outside the Advanced Actions method:

    Unicode method: string + '\u{000A}' + string

    HTML method: string + ''&#10;'' + string

    The break method has worked as well: string + '<br/> + string

    I've had sucess with the following: string + '\n' + string

     

    If you want it to work within an Advance Action....then create a new variable called anything you wish, I will call it nl for t this demo.....assign your new variable with the following text: '&#10;'

    Example:

     

     

     

     

     

     

     

     

    Now when you are wanting to add new lines to your strings, use the new variable you created and it will work like a charm!

     

    Example: v_teb_notes = "Hello" + nl + "world";

     

    Known Participant
    July 27, 2022

    Many thanks for your kind reply and examples.

    Unfortunately none works in JS in the TEB. The browser still returns "SyntaxError: '' string literal contains an unescaped line break". I can check the JS in an online verifier e.g. Code Beautify and it passes, but fails in Captivate.

     

    I then added the nl variable in Captivate, specifying the expression "v_teb_notes=v_teb_notes+nl" on my loading slide so that when the TEB displays the user should see the text "Initial thoughts" (the default value of v_teb_notes is "Initial thoughts") with the cursor on line 2 ready to accept input. The TEB displays "Initial thoughts'&#10;'" so CP is  appending the nl value rather than interpreting it as a new line.

     

    I then tried using the nl variable in the JS expression in place of the escape codes and this allowed the script to run successfully. However, the line breaks where not displayed in the TEB, only the literal characters i.e. '&#10;'

     

    This made me wonder if there is an issue with the TEB, so I added a text caption that displays v_teb_notes. With '&#10;' as the value the line breaks are added but with '' between the strings, so I changed the value to &#10;

     

    This displays correctly in the text caption but not in the TEB. The TEB appears to ignore the escaped line break.

    I have opened a ticket with Adobe, but if this is a bug it will probably take years to fix.

    I hope there is a solution!

    OH_CP_Lover_&_Hacker
    Inspiring
    July 27, 2022

    Ok....sorry my bad I did not know you were trying to update an actual TEB, I missed that when reading your question earlier.  Let me take a closer look at TEB for ya...be back in a while.  I also have another idea for you that might work very well too.  What is the size of your TEB by chance, as I want to mimic the exact setup you have so that I can do some testing for you.  Hope you ar having a great day.