JavaScript and boolean variables
Jun 10, 2015
I am trying to pass boolean variables (true/false) to and from storyline player (Flash and HTML5).
But is seems it only sends strings ("true" of "false"), and not actual boolean values. Thus send false is interprited by the player as a string, and evalutates to a true value.
Or am I doing something worng...
I have tried putting the valus in quotes on the javascript side, but the Articulate player seem inconsistent. Have also tried sending 1 and 0.
My code:
var player=GetPlayer();
var numOfSlides = player.GetVar("numberOfSlides");
var slideViewed = false;
//alert(numOfSlides);
for (var i=1; i <= numOfSlides; i++) {
slideViewed = player.GetVar("s" + i);
// alert (slideViewed);
if (slideViewed == "false") {
// alert("Not done" + i );
player.SetVar("allSlidesViewed", fasle);
break;
}
else {
// alert(" all Done" + i);
player.SetVar("allSlidesViewed", true);
}
}
6 Replies
Hi, Morten, and welcome to the community! :) Although we don't provide support for JavaScript coding, it may be helpful to take a look at this document on JavaScript Best Practices. Hopefully, some of our experts in the community can chime in with suggestions!
Not sure if this is your original code, but you misspelled false. "player.SetVar("allSlidesViewed", fasle);"
Also don't put parenthesis around false. This will interpret as a string. if (slideViewed == "false")
Also make sure that the variables in SL are set to true/false and not text.
Thanks for your input, Jerson! :)
Yes, I did look at the Best Pratice page, but that does not cover boolean variables, and how they are passed.
Yes, Jerson, that misspelled "false" was just in this post, as i did some tweaking before I sent it in a hurry. :-)
The Storyline variable i set as a True/False.
I had to put quotes around "false" in the if-statemenet for it to work. If I wrote it like I naturally would:
slideViewed = player.GetVar("s1");
if (slideViewed == false) {...}
- the stament was alway true. That suggested to me that the value inside slideViewed is not a boolean value of false, like it should be, but a text string with the text "false".
So the question remains; If you get, or set, a boolean value from a variable inside the SL-player, do you get a proper boolen value, or just a text string with "true" or "false"?
Also the other way around, when passing a value to the player, I would natuarlly use the boolean value. eg.
player.SetVar("allSlidesViewed", fasle);
Will this be properly stored in the Storyline True/False variable? It seems a bit inconsitent between Flash and HTML5 player...
Hi Morten
I was having to work with booleans in Javascript today and came across this thread. It does appear that storyline booleans when used in javascript equate to a string with "true" or "false" as the value, as I couldn't get a simple if (var == true) to work (or rather I could but I couldn't get the else to work as intended), but could with a if (var == "true"). Hope this helps anyone else.
Yes, it is only string values. If I remember correctly, I may ended up using numbers instead. Integer 1 or 0 - but I'm not sure.
This discussion is closed. You can start a new discussion or contact Articulate Support.