JavaScript interacts with .NET applications_Experiment 1
C# calls JavaScript functions
to . A few examples are given below to facilitate everyone’s learning and communication.
There are many JavaScript engines currently, including Google’s Google Chrome V8, Noesis.Javascript, Jurassic.ScriptEngine and so on. As a result of the test, Jurassic.ScriptEngine is more convenient.
The following takes Jurassic.ScriptEngine as an example to introduce a few small examples.
Example 1, calling JavaScript functions in C#
(1) Create a new C# Windows project
(2) Reference the Jurassic library
The picture after the reference is completed: (3) Add 2 TextBox and 1 Button on WinForm. X TextBox is used to enter the JS code, and the results displayed by the JS calculation. The interface layout is as shown in the figure: The corresponding code is as follows: The corresponding codevar is a weakly typed object in C#, newly added since the .NET3.5 version. Detailed instructions can be found on MSDN.引 First load the JS code in Textbox1 through the script engine, and then call the C#call to the program that has been loaded to the JS engine by calling the global function.
JS code:JS code:
function add (a,b){
Return (a+b)*2;}
function main(a, b){
var x=add(a,b);return x;
}Running effect (2)
Summary: in the script.