Kiffi Well-Known Member Licensed User Longtime User Apr 20, 2014 #1 Hello, calling a JS-Function from a B4J-Websocket-Server, that is in the window-scope (with ws.RunFunction("myTest", Args)) works like a charm: Code: Copy $(function () { $(document).ready(function () { [...] }); }); function myTest() { // no problem here }; but i don't know how to call a JS-Function, that is not in the window-scope: Code: Copy $(function () { $(document).ready(function () { [...] function myTest() { // i don't know how to call this function // i get 'undefined is not a function' }; }); }); because b4j_runFunction() wants to apply a function in the window-scope: Code: Copy function b4j_runFunction(func, params) { return window[func].apply(null, params); } Any ideas how to solve it? Thanks in advance & Greetings ... Kiffi
Hello, calling a JS-Function from a B4J-Websocket-Server, that is in the window-scope (with ws.RunFunction("myTest", Args)) works like a charm: Code: Copy $(function () { $(document).ready(function () { [...] }); }); function myTest() { // no problem here }; but i don't know how to call a JS-Function, that is not in the window-scope: Code: Copy $(function () { $(document).ready(function () { [...] function myTest() { // i don't know how to call this function // i get 'undefined is not a function' }; }); }); because b4j_runFunction() wants to apply a function in the window-scope: Code: Copy function b4j_runFunction(func, params) { return window[func].apply(null, params); } Any ideas how to solve it? Thanks in advance & Greetings ... Kiffi
warwound Expert Licensed User Longtime User Apr 20, 2014 #2 I'm not a user of jquery (that is jquery syntax i see you using?) but can you try something like this: Code: Copy window.myTest=function(){ alert('Hello World'); } Martin. (Edited to correct function name) Upvote 0
I'm not a user of jquery (that is jquery syntax i see you using?) but can you try something like this: Code: Copy window.myTest=function(){ alert('Hello World'); } Martin. (Edited to correct function name)
Erel B4X founder Staff member Licensed User Longtime User Apr 20, 2014 #3 The function you created is not accessible from outside (unless you follow @warwound suggestion): https://developer.mozilla.org/en/do..._function_scope#Nested_functions_and_closures Upvote 0
The function you created is not accessible from outside (unless you follow @warwound suggestion): https://developer.mozilla.org/en/do..._function_scope#Nested_functions_and_closures
Kiffi Well-Known Member Licensed User Longtime User Apr 20, 2014 #4 Hello warwound, hello Erel, thanks for your fast replies! @warwound: your solution works but it is unfortunately impractical for me because then the function is not accesible from the other 'inner' functions. @Erel: Thanks for the background informations! Perhaps i use b4j_eval()... Thanks again & Greetings ... Kiffi Upvote 0
Hello warwound, hello Erel, thanks for your fast replies! @warwound: your solution works but it is unfortunately impractical for me because then the function is not accesible from the other 'inner' functions. @Erel: Thanks for the background informations! Perhaps i use b4j_eval()... Thanks again & Greetings ... Kiffi
Erel B4X founder Staff member Licensed User Longtime User Apr 20, 2014 #5 WebSocket.RunEval will not help here. The inner function is not accessible from outside of the outer function. You cannot call it from JavaScript either. Why don't you make it a regular function? What is it needed for at all? Upvote 0
WebSocket.RunEval will not help here. The inner function is not accessible from outside of the outer function. You cannot call it from JavaScript either. Why don't you make it a regular function? What is it needed for at all?
warwound Expert Licensed User Longtime User Apr 20, 2014 #6 Kiffi said: @warwound: your solution works but it is unfortunately impractical for me because then the function is not accesible from the other 'inner' functions. Click to expand... You could declare all functions locally and then create global references to any of these (local) function(s) that require global scope: Code: Copy function myTest(){ alert('Hello World'); } function doSomething(){ console.log('Hello World'); } window.myTest=myTest; Martin. Upvote 0
Kiffi said: @warwound: your solution works but it is unfortunately impractical for me because then the function is not accesible from the other 'inner' functions. Click to expand... You could declare all functions locally and then create global references to any of these (local) function(s) that require global scope: Code: Copy function myTest(){ alert('Hello World'); } function doSomething(){ console.log('Hello World'); } window.myTest=myTest; Martin.
Kiffi Well-Known Member Licensed User Longtime User Apr 21, 2014 #7 warwound said: You could declare all functions locally and then create global references to any of these (local) function(s) that require global scope: [...] Click to expand... Perfect! Works like a charm. Thank you very much! Erel said: What is it needed for at all? Click to expand... i have written a tool that converts PureBasic to JavaScript (PB2Web). And now i want to implement an interface to connect a B4J-Websocket-Server. The first steps are very promising. Greetings ... Kiffi Upvote 0
warwound said: You could declare all functions locally and then create global references to any of these (local) function(s) that require global scope: [...] Click to expand... Perfect! Works like a charm. Thank you very much! Erel said: What is it needed for at all? Click to expand... i have written a tool that converts PureBasic to JavaScript (PB2Web). And now i want to implement an interface to connect a B4J-Websocket-Server. The first steps are very promising. Greetings ... Kiffi
Erel B4X founder Staff member Licensed User Longtime User Apr 22, 2014 #8 Note that the last step is not required. You just need to declare the functions. They will be declared in the window scope. Upvote 0
Note that the last step is not required. You just need to declare the functions. They will be declared in the window scope.
Kiffi Well-Known Member Licensed User Longtime User Apr 22, 2014 #9 Hello Erel, this is, what i'm doing now: Code: Copy $(function () { $(document).ready(function () { [...] function myTest() { [...] }); window.myTest = myTest; }); }); and now i'm able to access the function with: Code: Copy ws.RunFunction("myTest", Args) Thanks again & Greetings ... Peter Upvote 0
Hello Erel, this is, what i'm doing now: Code: Copy $(function () { $(document).ready(function () { [...] function myTest() { [...] }); window.myTest = myTest; }); }); and now i'm able to access the function with: Code: Copy ws.RunFunction("myTest", Args) Thanks again & Greetings ... Peter
Erel B4X founder Staff member Licensed User Longtime User Apr 22, 2014 #10 This is a good solution. This code will also work: Code: Copy $( document ).ready(function() { b4j_connect("..."); }); function myTest() { ... } Upvote 0
This is a good solution. This code will also work: Code: Copy $( document ).ready(function() { b4j_connect("..."); }); function myTest() { ... }