Page 1 of 1

exeJavascriptDirect

PostPosted: Thu Mar 26, 2015 1:19 pm
by John Robin Dove
Hi Clifton,

Here's today's problem:

I want to execute this:

var num = X;
if (isNaN(num) == false) { return true;} else {return false;}

I replace X with the appropriate number using pgReplace.

I've tried with and without the curly braces. Surprisingly (to me) it will work without them in a normal html. But not in the Toolbook DHTML.

I haven't found pgIsNumeric either :D

Re: exeJavascriptDirect

PostPosted: Thu Mar 26, 2015 1:28 pm
by John Robin Dove
Don't worry I've found a solution that works:

"var num = X; var result;if (isNaN(num) == false) result = 'true'; else result = 'false'; return result;"
...

if result = "true" etc.

Perhaps it might help someone else.

Re: exeJavascriptDirect

PostPosted: Thu Mar 26, 2015 1:45 pm
by Clifton
The PowerPac uses an internal function called pgIsNumber() to handle this scenario.

pgIsNumber() fixes the flawed ToolBook isNumber() action and becomes the alias for isNumber() whenever you use it in the Actions Editor.

But if you want to use exeJavascriptDirect(), simply enter:
"return pgIsNumber(x);"
... where [x] is the value to test.

RECOMMENDED INSTEAD:
Better to just use isNumber(x) as an Action in the Actions Editor because it is really calling the internal PowerPac function anyway. This way you don't have to use pgReplace() to get your value into the string expression for x.

NOTE: pgIsNumber() handles all the possible JavaScript object types that would otherwise return true in your scenario.

ALSO, don't check the result as a string "true" or "false". Just check for boolean true or false without the quotes.

Re: exeJavascriptDirect

PostPosted: Fri Mar 27, 2015 4:45 am
by John Robin Dove
Hi Clifton,

Thanks for your reply. So there is a built in isNumeric function after all. I still can't figure out how to use it without exeJavascriptDirect(). What action should I insert so that I can call isnumber()?

Re: exeJavascriptDirect

PostPosted: Fri Mar 27, 2015 7:09 am
by Clifton
The PowerPac overwrites the isNumber() action in the actions system when the book is exported by replacing with pgIsNumber().

So you can just type it like a few other commands in the Actions Editor:

Image 2.png
Image 2.png (3.23 KiB) Viewed 3273 times

Previously, when you used the above code in the Action Editor, it would throw an alert indicating the number is not a number instead of gracefully moving to the next branch of the IF / THEN statement. The alert was ugly and the results of ToolBook's internal isNumber() were flawed besides.

Re: exeJavascriptDirect

PostPosted: Fri Mar 27, 2015 7:54 am
by John Robin Dove
Right. Thanks very much. A lot simpler than I imagined! And just what I need.