Idea:
Make a JavaScript function on a 'global' level or even a library of Javascript functions that can be used from .sb code.
Contemplate following .sb code:
Code: Select all
EnableJS
function someJSfunction(text)
{
console.log(text);
}
DisableJS
; Function adapter
Procedure myCall(text.s)
Debug "calling myCall"
EnableJS
someJSfunction(v_text);
DisableJS
Debug "finished myCall"
EndProcedure
;Actually make a function call
Debug "start"
myCall("hello world")
Debug "end"
When you run the code in debug window you get
then execution stops.start
calling myCall
It is expected to print "Hello World" in browser console and come to an 'end' in Debug window.
Inspect browser console. You get:
Inspect compiled JavaScript code:ReferenceError: someJSfunction is not defined
Code: Select all
// fragment of Javascript as compiled
function f_mycall(v_text) {
spider.debug.Print(_S1);
someJSfunction(v_text);
return 0;
}
//
SpiderLaunch = function() {
spider.debug.Init();
spider_InitFunctions();
function someJSfunction(text)
{
console.log(test);
}
spider.debug.Print(_S2);
f_mycall(_S3);
spider.debug.Print(_S4);
}
Found out that the problem is the 'position' or 'location' of the function (or perhaps we can call it 'closure' where compiled code is placed as it is placed in logical place of 'global' code enclosure )
Hack the javascript compilation to (take function out from the 'global' enclosure:
Code: Select all
// Hacked spiderbasic.js
function someJSfunction(text)
{
console.log(test);
}
function f_mycall(v_text) {
spider.debug.Print(_S1);
someJSfunction(v_text);
return 0;
}
//
SpiderLaunch = function() {
spider.debug.Init();
spider_InitFunctions();
spider.debug.Print(_S2);
f_mycall(_S3);
spider.debug.Print(_S4);
}
Debug window:
Browser console:start
calling myCall
finished myCall
end
Code: Select all
start debug.js:47:13
calling myCall debug.js:47:13
hello world spiderbasic.js:273:9
finished myCall debug.js:47:13
end
experiments...
Move EnableJS block up and down in the .sb code... it looks like it renders it in the same place always regardless where 'global' code is placed in actual .sb code.
Solution 1: It would be nice to have 'global' JS block before any function compiled OR
Solution 2: better!!, place global JS block in order of appearance in the code - if it is between two procedures, place it there. This way coder can make JS code visible or not according to the placement.
or (perhaps) solution 3: expand EnableJS statement to influence code placement in resulting (compiled) Javascript e.g. one of examples could be to make "EnableJSLibrary" that would render library of JS functions before procedures in .sb code.
Note: It is possible to influence compiling/rendering of JS code by placing EnableJS within Procedure (a technique already used in the sample in 'adapter'), but then it becomes enclosed within Procedure function (JS function is not 'global' in this case and cannot be used)
p.s. EnableExplicit makes no difference (there is no violation of .sb code rules)
perhaps there may be some other way of influencing this block I do not know about.
Cheers!