Skip to content
oliverbock edited this page Dec 5, 2018 · 3 revisions

Quick Start

This section provides documentation to get quickly started to embed and run Javascript .NET in your application. See Reference for details.

Download Javascript .NET

Download the Javascript .NET latest binary release here. The contained Noesis.Javascript.dll assembly and the Microsoft Runtime Library files in the same directory are all that is required to run Javascript .NET.

Create a new project

Open Visual Studio and create a new Console application.

Reference the Noesis.Javascript assembly from your project

Add a reference to Noesis.Javascript.dll in your project by right clicking on the project and choosing "Add Reference", then select the "Browse" tab, locate the appropriate Noesis.Javascript.dll assembly and click "OK".

Using Noesis.Javascript

Add the following to the "using" statements at the top of you program file:

using Noesis.Javascript;

Initialize a Javascript context

JavascriptContext context = new JavascriptContext()

(Don't forget to Dispose() this object when you are finished with it.)

Set variables in the Javascript context's global scope

SetParameter(string iName, Object iObject);

 context.SetParameter("console", new SystemConsole());
 context.SetParameter("message", "Hello World !\n");

Simple values and arrays will be converted into native JavaScript objects. Classes are wrapped, and uses of their methods and properties will be forwarded to the original .Net object.

Get a parameter from the Javascript context

GetParameter(string iName);

 context.GetParameter("number");

Compile and run the Javascript: Run(string iScript);

context.Run("var i; for (i = 0; i < 5; i++) console.Print(message + ' (' + i + ')'); number += i;");

"Hello World" quick integration sample

A simple integration is shown below. In the sample, a context is created to which a CLI object is supplied to expose the console to the Javascript. A string and an integer are also supplied on which elementary operations are performed. Finally, the resulting value of the supplied integer value is extracted and printed to the console.

Code

    class Program
    {
        public class SystemConsole
        {
            public SystemConsole() { }

            public void Print(string iString)
            {
                Console.WriteLine(iString);
            }
        }

        static void Main(string[] args)
        {
            // Initialize the context
            using (JavascriptContext context = new JavascriptContext()) {

                // Setting the externals parameters of the context
                context.SetParameter("console", new SystemConsole());
                context.SetParameter("message", "Hello World !");
                context.SetParameter("number", 1);

                // Running the script
                context.Run("var i; for (i = 0; i < 5; i++) console.Print(message + ' (' + i + ')'); number += i;");

                // Getting a parameter
                Console.WriteLine("number: " + context.GetParameter("number"));
            }
        }
    }

Output

The following will be outputed by the program above:

Hello World ! (0)
Hello World ! (1)
Hello World ! (2)
Hello World ! (3)
Hello World ! (4)
6