Page 1 of 1

exeJavascriptDirect

PostPosted: Mon Dec 01, 2014 7:07 am
by John Robin Dove
Hi,

I'm trying to get the path to a certain folder with exeJavascriptDirect. I know the Javascript is correct because the alert displays the path I want but I can't transfer it to the TB environment. I've tried the following:

{
var url = window.location.href;
var arr = url.split("/");
var path = arr[0] + "//" + arr[2] + "/" + arr[3];
//alert(path);
tbfunction_pgTBObjSet("showResult", "text", path);
}

{
var url = window.location.href;
var arr = url.split("/");
var path = arr[0] + "//" + arr[2] + "/" + arr[3];
//alert(path);
return path;
}

{
var url = window.location.href;
var arr = url.split("/");
var path = arr[0] + "//" + arr[2] + "/" + arr[3];
//alert(path);
return [path];
}

but none of them work.

Re: exeJavascriptDirect

PostPosted: Mon Dec 01, 2014 8:02 am
by Clifton
Try removing the object definition AND make sure you have a field named "showResult"

var url = window.location.href;
var arr = url.split("/");
var path = arr[0] + "//" + arr[2] + "/" + arr[3];
//alert(path);
tbfunction_pgTBObjSet("showResult", "text", path);

Re: exeJavascriptDirect

PostPosted: Mon Dec 01, 2014 8:55 am
by John Robin Dove
Hi Clifton,

I definitely have a field called "showResult". I'm afraid I don't understand what you mean by 'removing the object definition'. I notice you removed the braces and I tried without them but I still just get undefined in the field.

Re: exeJavascriptDirect

PostPosted: Mon Dec 01, 2014 9:12 am
by John Robin Dove
I have fixed it. I think using single quotes in the javascript code solved the the problem but I'm not 100% sure. There may have been some other error. Thanks anyway.

Re: exeJavascriptDirect

PostPosted: Mon Dec 01, 2014 3:18 pm
by Clifton
Generally, the curly braces before and after your code may give unpredictable results with exeJavascriptDirect() because you are essentially defining an object. When the function executes, it just happens to execute each line of the code when building the object. However, this is not really the recommended way to build your code.

Drop the braces or wrap them in a function call like this:
var fct = function() {
code;
}
//Then call it this way
fct();

(NOTE: only problem with the function call method is that you cannot have a return statement inside the function or exeJavascriptDirect() will complain about it. There are workarounds like defining a return variable and having the function set it as shown below:
var rtn;
var fct = function() {
code;
rtn = (put function return value in global scope outside of function);
};
return fct(); //executes the function and the variable rtn will be set and returned back to the ToolBook application

But really just enter you lines of code without curly braces and all should be good. My guess is that if your code was entered with a word processor, then the double quotes had been replaced by utf-8 equivalents. When you changed them to single quotes, you fixed the syntax and then it worked. The truth is double and single quotes work with exeJavascriptDirect() when the code is entered in a field of its own.