Skip to main content
Known Participant
September 7, 2017
Question

Copying and pasting javascript in advance actions not working

  • September 7, 2017
  • 11 replies
  • 2105 views

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 has been closed for replies.

11 replies

digitaly
Participating Frequently
October 26, 2018

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.

Inspiring
June 14, 2018

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.

arashmazAuthor
Known Participant
September 13, 2017

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.

Brainiac
September 12, 2017

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.

arashmazAuthor
Known Participant
September 11, 2017

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

papaintegrator
Inspiring
September 11, 2017

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.

arashmazAuthor
Known Participant
September 11, 2017

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

papaintegrator
Inspiring
September 11, 2017

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

dan_tasota
Inspiring
September 8, 2017

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.

digitaly
Participating Frequently
October 26, 2018

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.

Brainiac
September 8, 2017

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.

Lilybiri
Brainiac
September 8, 2017

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

arashmazAuthor
Known Participant
September 8, 2017

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.

Lilybiri
Brainiac
September 8, 2017

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.

arashmazAuthor
Known Participant
September 8, 2017

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.

Lilybiri
Brainiac
September 8, 2017

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.