exeJavascriptDirect

Help for using specific functions

exeJavascriptDirect

Postby John Robin Dove » Thu Mar 26, 2015 1:19 pm

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
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am

Re: exeJavascriptDirect

Postby John Robin Dove » Thu Mar 26, 2015 1:28 pm

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.
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am

Re: exeJavascriptDirect

Postby Clifton » Thu Mar 26, 2015 1:45 pm

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.
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am

Re: exeJavascriptDirect

Postby John Robin Dove » Fri Mar 27, 2015 4:45 am

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()?
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am

Re: exeJavascriptDirect

Postby Clifton » Fri Mar 27, 2015 7:09 am

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 1740 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.
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am

Re: exeJavascriptDirect

Postby John Robin Dove » Fri Mar 27, 2015 7:54 am

Right. Thanks very much. A lot simpler than I imagined! And just what I need.
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am


Return to Function Help

Who is online

Users browsing this forum: No registered users and 1 guest

cron