exeJavascriptDirect

Workarounds and usability notes.

exeJavascriptDirect

Postby John Robin Dove » Mon Dec 01, 2014 7:07 am

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

Re: exeJavascriptDirect

Postby Clifton » Mon Dec 01, 2014 8:02 am

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

Re: exeJavascriptDirect

Postby John Robin Dove » Mon Dec 01, 2014 8:55 am

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

Re: exeJavascriptDirect

Postby John Robin Dove » Mon Dec 01, 2014 9:12 am

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

Re: exeJavascriptDirect

Postby Clifton » Mon Dec 01, 2014 3:18 pm

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


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 4 guests

cron