Skip to main content
Known Participant
Sep 7, 2017 4:24 PM UTC
Question

Copying and pasting javascript in advance actions not working

Hi

I'm using Captivate 9 and have recently run into a bug or something.

I've created a basic slide to allow me to test some Javascript. I have the Javascript working as it should, assigned to a smart shape button.

I wanted to add the code to a conditional action but when I copy and paste the Javascript in the script window inside a conditional action and select ok, then re-open the script window all the code has disappeared.

It's not a massive amount, but it would be quite tedious to type from scratch. I tried typing in a bit of code and closing the script window and then re-opening it and the typed code stays in. It seems if you paste code into the script window within a conditional action it doesn't stay there.

This seems really bizarre. Has anyone else had any similar experiences?

This topic is closed to new replies. Start a new post to keep the conversation going.

21 replies

digitaly
Participating Frequently
Oct 26, 2018 9:21 PM UTC

I still find this annoying bug to be occurring in Captivate CC 2015. Try switching all your < characters to &lt; in your text editor before pasting into the code window. It should accept your code and replace the HTML entities with the <. But you have to do the switch again if you make any edits to your code.

Translate
Inspiring
Jun 14, 2018 8:33 PM UTC

I find that code that is not accepted in an AA execute js box IS accepted in an On Enter js box. So, I put longer functions in the On Enter, then call them with a line or two from the AA execute js. This true on copy/paste and straight typing.

Translate
arashmazAuthor
Known Participant
Sep 13, 2017 2:27 PM UTC

Hey papaintegrator​ yeah I tried it with your suggestion and it works perfectly. Thanks for helping out.

Still baffles me it doesn't accept a certain character.

Translate
Legend
Sep 12, 2017 3:53 PM UTC

In other software like Articulate's Storyline, the code that you type into their code box is actually output into a separate .js file on publish.  I really wish Cp would do the same.  It would allow one to use less than signs, regular expressions and inject HTML markup.  You can include a .js file on publish in Cp, but you have to manually set that up.  It's a once and done... from there after, Cp will include your .js file automatically.  It's a bit of a kluge, but it works.  I'm really hoping they enhance this feature in the next version.

Translate
arashmazAuthor
Known Participant
Sep 11, 2017 10:07 AM UTC

Hey thanks for fixing!

I'll give it a try in a moment. Is there a reason Captivate does this or is it simply just a software quirk?

Thanks

Translate
papaintegrator
Inspiring
Sep 11, 2017 10:18 AM UTC

This seems to be a bug or may be they have disabled it due to some reason. I have logged a request for this a long time ago (in 2016) but haven't heard back on the status since then. It's about time I check back.

FYI for all: Issue reproduces in Captivate 2017.

Translate
arashmazAuthor
Known Participant
Sep 11, 2017 8:12 AM UTC

Yeah Lilybiri is correct - that's exactly the question.

It does have '<' characters in. Why do these cause a problem?

The code is -

var result

var userChoice = window.cpAPIInterface.getVariableValue('Var_UserChoice');

var computerChoice = Math.random();

if (computerChoice < 0.34) {

  computerChoice = "rock";

} else if(computerChoice <= 0.67) {

  computerChoice = "paper";

} else {

  computerChoice = "scissors";

}

var compare = function (choice1, choice2) {

    if (choice1  === choice2) {

        result = "The result is a tie!";

    }

    else if (choice1 === "rock") {

         if (choice2 === "scissors"){

            result = "rock wins";

        }

        else {

            result = "paper wins";

        }

       

    }

    else if (choice1 === "paper") {

           

             if (choice2 === "rock") {

                 result = "paper wins";

             }

             else {

                 result = "scissors wins";

             }

    }

    else if (choice1 === "scissor") {

           

             if (choice2 === "rock") {

                 result = "rock wins";

             }

             else {

                 result = "scissors wins";

             }

    }

};

compare(userChoice, computerChoice);

window.cpAPIInterface.setVariableValue('Var_Result', result);

window.cpAPIInterface.setVariableValue('Var_CPU', computerChoice);

Thanks

Translate
papaintegrator
Inspiring
Sep 11, 2017 8:26 AM UTC

As Jim also pointed out, Cp may reject some of the characters.. so while you can type them.. it may not accept them upon pasting. I have encountered this problem earlier with < and > where they worked when typed but didn't when pasted. I will check with the code you have provided and see if I get the same behavior.

In some of the cases, pasting the code to notepad first and then copying from there to Captivate have worked as this may be the issue of unicoded characters.

/best

Translate
dan_tasota
Inspiring
Sep 8, 2017 5:31 PM UTC

Does your code have a for loop?  I've noticed that you can use a for loop on a button, but Captivate doesn't seem to allow you to use a for loop inside of an advanced action.

Translate
digitaly
Participating Frequently
Oct 26, 2018 9:30 PM UTC

I think the only problem with for loops in an advanced action script window is that most for loops have a < character in the condition part of the declaration (e.g. i < str.length);

I've used for loops in AA using the hack described above.

Translate
Legend
Sep 8, 2017 5:16 PM UTC

Can you post the full code so we can see if there's anything the Cp JS Editor might reject?  It's funny about certain characters.

Translate
Lilybiri
Legend
Sep 8, 2017 5:24 PM UTC

Jim, I mentioned that as well. But it was possible to type in the same code, was only a problem when using copy/paste.

Translate
arashmazAuthor
Known Participant
Sep 8, 2017 3:59 PM UTC

It's still not working correctly.

Just to clarify the javascript code works in captivate already when you create a button and use the 'execute Javascript' action. I can copy and paste my code in that window without any issues.

But if I do the exact same thing within an Advanced Action or Conditional Action the code pastes in the script window. I click OK. Go back into the script window and the code has disappeared.

It's weird it's not consistent with how it works across the software. I noticed it's the same in Captivate 8.

Translate
Lilybiri
Legend
Sep 8, 2017 4:22 PM UTC

I understand, but the example for which I offered a link, had that JS in a command line of an advanced action? Would love to double-check but have only a license for CP2017.

Translate
arashmazAuthor
Known Participant
Sep 8, 2017 8:23 AM UTC

Thanks.

Yeah I'm on 9.0.2.437 and I cleared my settings in the utilities folder.

It might have just been a random occurrence I'll have another go and see if it works any better.

Translate
Lilybiri
Legend
Sep 8, 2017 8:27 AM UTC

Please, check it out again. I know that more complicated JS scripts are better stored in an external JS file than in the Captivate window, but your problem was not with the complexity but only with the copy/paste instead of typing, correct? And I suspect that the copied text was totally unformatted since you are a coder.

Translate