Copy link to clipboard
Copied
I have an action which executes JavaScript. In the script window I enter (as a quick test)
/\d/.test("123")
and it wrongly returns false. If instead I replace the backslash with four backslashes
/\\\\d/.test("123")
it correctly returns true.
Does this mean that Captivate is processing the script window contents, to maybe save it as a string, and then losing the intended meaning for a single backslash when it reads it back?
Should I only be using external JavaScript files?
Copy link to clipboard
Copied
You are correct. I always use external JavaScript files.
Copy link to clipboard
Copied
I know this question was asked a long time ago and for Captivate 9 (I believe), but I ran into this same issue in Captivate 2019 (still an issue many versions later). After banging my head against the wall for some time, I finally discovered a solution (in Captivate 2019 at least).
In order to get a backslash (\) to show up from code entered in the Javascript window within Captivate, you need 8... yes 8 backslashes for every 1 backslash you want to show up. I think this has to do with the escaping of the text when Captivate saves the code from the Javascript window.
For example, if you were to type the following in the Javscript window inside Captivate 2019:
console.log("here is a \ (backslash)");
you would not see the actual \ however, if you type:
console.log("here is a \\\\\\\\ (backslash)"); // remember, you need 8 backslashes for every 1 you want to show up.
you will see a single backslash in the output of the browser console.
I know that I could put the script in an external Javascript file (and it would work fine with a single backslash), but that would require me to publish out the project, add the external js file to the project folder, edit the index.html file to add the include statement for the external Javascript file... etc. When you are prototyping or in development, you need to be able to just preview the project as HTML5 in the browser without having to go through the whole publish process every single time you need to make sure everything is working.
I hope this helps someone out there, because it was driving me crazy for a long time.
Copy link to clipboard
Copied
I imagine it's because JavaScript is in a string, Captivate escapes some of it, but doesn't like certain characters, the < symbol included.
Are you doing division in the script?
Copy link to clipboard
Copied
No, actually I am trying to use regular expressions and need to be able to use things like \s to find whitespaces, or \w to find word characters, or \d to find numbers, etc.
One of my regular expressions is: (^|\s)test($|\s) which tries to find the word test by itself, or surrounded by spaces. That regExp works fine in regular JavaScript files, but when entered into the JavaScript window within Captivate, all of the backslashes are removed. I finally figured out that if I change my RegExp to: (^|\\\\\\\\s)test($|\\\\\\\\s), it works in Captivate as expected.
As far as division goes, you should be fine because the / does not seem to be removed. so something like: var ans = 1/2; // should work fine.