• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Copying and pasting javascript in advance actions not working

Participant ,
Sep 07, 2017 Sep 07, 2017

Copy link to clipboard

Copied

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?

TOPICS
Advanced

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 07, 2017 Sep 07, 2017

Copy link to clipboard

Copied

I cannot really help you because I have no longer a CP9 license. However in this article I did paste JS in the JS window and it is in a conditional action (scroll to the end of the article):

Playing with Numbers - part 2 - Captivate blog

It was created with 9.0.2.437. Do you use that latest version of 9?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 08, 2017 Sep 08, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 08, 2017 Sep 08, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 08, 2017 Sep 08, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 08, 2017 Sep 08, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 09, 2017 Sep 09, 2017

Copy link to clipboard

Copied

Watch out for characters < and >. Do you have any of these in the code?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 10, 2017 Sep 10, 2017

Copy link to clipboard

Copied

The question is : why is it possible to type in that code, but not to copy / paste. If there are characters or loops, typing in would cause problems as well or not?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Sep 08, 2017 Sep 08, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 08, 2017 Sep 08, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 08, 2017 Sep 08, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 26, 2018 Oct 26, 2018

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 11, 2017 Sep 11, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 11, 2017 Sep 11, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 11, 2017 Sep 11, 2017

Copy link to clipboard

Copied

Hi,

I have used your code and as suspected the issue was due to the < used in the code. Instead use > and reverse the argument like this:

var result

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

var computerChoice = Math.random();

if (0.34 > computerChoice) {

  computerChoice = "rock";

} else if(0.67 >=  computerChoice) {

  computerChoice = "paper";

} else {

  computerChoice = "scissors";

}

This should work fine now.

/best

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 11, 2017 Sep 11, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 11, 2017 Sep 11, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 12, 2017 Sep 12, 2017

Copy link to clipboard

Copied

Hi,

Did you have a chance to try the solution? Let us know if this worked for you.

/best

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Sep 12, 2017 Sep 12, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 13, 2017 Sep 13, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 14, 2018 Jun 14, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 26, 2018 Oct 26, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Help resources