Adding Javascript function file gives errors

Hi

I want to use a javascript function to generate a client side id. So I added a test,js file in the Extra files of my HTTP protocol script, and added a line with

#include "test.js"

to the globals.h file. I also enabled javascript for the test in the run settings.

Then I tried it by adding this very simple function(see end of the post) to the test,js file. But when I compile this, it gives the error : syntax error; found `myFunction' expecting `;'

Clearly it does not like the function file, but I dion't see hwat is the problem. Mind, I am not calling the function from anuwhere in the test at this point, so that can not be the issue...Anybody some advice? I am not a vey experienced javascript programmer, so maybe I am forgetting something obvious, but I would like to learn what.

 function myFunction(a, b) {
    return a * b;
}

  • Hi KajFeis,
    According to my understanding, when you first create a Web project, it's C based project. If you want to change it to Javascript based project, please follow below steps:

    1. create a new web project. (at this time, the project is C based)

    2. go to "Recording Options" from the "Record" file menu, change the language option to "Javascript"record_option.png

     3. Do a simple recording, if you don't want any steps, you can just record a "about:blank" page then stop. After the recording, your project will be turned to a "Javascript based" project, like belowjs_pro.png

    4. then you can create your test.js with your function under the Extra Files, and require your js file in the globals.js:

     

    require.png

     

    5. After that, you can call your javascript function from the Web script's action view:

     

    action.png

     

    Hope this is what you want! 

    -Penn

     

     

     

     

  • Hi Penn,

    I was hoping that I could use a javascript library inside my C-based script...the documentation gave me the idea that this was possible. And it would be a very nice feature, given that string manipulations etc are much easier in javascript than C....

    But if i understand you corectly, then this is only possible if I start with Javascript as my scripting language from the creation of the script. .

  • Verified Answer

    Hello KajFeis,

    You can run JavaScript code within a C language Web-HTTP/HTML script, using the web_js_run API.

    The error you receive means that the C compiler is unable to compile the file content.
    Javascript code cannot be compiled by the the C compiler and does not need compilation anyway.
    Bottom line: there is no need to '#include' the .js file in you globals.h file.

    Other steps you took are a great start (configuration, extra files).
    Now just add a call to your code using web_js_run.

    Edo

  • Hi KajFeis,

    my first post will make you create a pure javascript- based web project, if you want C base project, you need to use the web_js_run API as Edo's suggestion, a sample for you, hope it can help!

     web_js_run.png

     

     -Penn

  • Yes! I removed the #include and it compiles beautifull! Thanks to you both for the answers, I always wanted to have some easier way to do certain things like string anipulation that are very difficult in C compared to Javascript, so I look forward to diving into this!