Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling C functions inside javascript

The javascript is printing out the HTML onto the page example below, is it possible to call a C function on it for example in C to convert something to another language there is a function LANG_Str("text") which converts the text into the specified language. Would it be possible to use this function on the below text inside Javascript?.

"<tr><th>Service</th><th>Target Allocation (%)</th><th></th>"

EDIT:

I'm basically wanting to do a human language translation. The site already supports multi-language, the problem is on the custom screen like the one shown above which gets generated in Javascript, cannot use the function used to translate text the way its done normally in C.

like image 397
ahmet Avatar asked Jun 21 '12 13:06

ahmet


People also ask

What is $() in JavaScript?

The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document. getElementById("id_of_element").

What is module Ccall?

Module.ccall() calls a compiled C function from JavaScript and returns the result of that function. The function signature for Module.ccall() is as follows: ccall(ident, returnType, argTypes, args, opts) You must specify a type name for the returnType and argTypes parameters.


1 Answers

If it's running in the browser: no. Sorry.

You might be able to do it in server-side code beforehand (e.g. Python or PHP which can call C) when putting together the page content. Alternatively you can make an AJAX request to a server which exposes the C function as an HTTP API/Endpoint (via, GCI, FCGI or Python/PHP/Perl). But not in the browser.

This is because the JS runs in a sandboxed virtual environment which has no access to system calls or anything outside the runtime.

EDIT

In response to your comment "The script is ran in the C using HTML_WriteToCgi", this suggests that you are putting together the HTML in C on your server. If this is correct, go for my option 1 above, by injecting the values directly into the JS source code if all values come out of some data known by the server.

You might consider moving some functionality out of browser JS and back into server-side code to solve your problem.

like image 91
Joe Avatar answered Sep 28 '22 00:09

Joe