Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load a JavaScript file with Jint in C#?

I want to load a JavaScript file using Jint, but I can't seem to figure it out. The documentation said I could do something like engine.run(file1), but it doesn't appear to be loading any files. Do I need to do something special with the file name?

Here's my JavaScript file:

// test.js
status = "test";

Here's my C#

JintEngine js = new JintEngine();
js.Run("test.js");
object result = js.Run("return status;");
Console.WriteLine(result);
Console.ReadKey();

If I put in code manually in Run it works.

object result = js.Run("return 2 * 21;"); // prints 42
like image 779
Daniel X Moore Avatar asked Aug 14 '11 19:08

Daniel X Moore


2 Answers

I found a workaround by manually loading the file into text myself, instead of loading it by name as mentioned here: http://jint.codeplex.com/discussions/265939

        StreamReader streamReader = new StreamReader("test.js");
        string script = streamReader.ReadToEnd();
        streamReader.Close();

        JintEngine js = new JintEngine();

        js.Run(script);
        object result = js.Run("return status;");
        Console.WriteLine(result);
        Console.ReadKey();
like image 139
Daniel X Moore Avatar answered Oct 04 '22 21:10

Daniel X Moore


Personally I use:

js.Run(new StreamReader("test.js"));

Compact and just works.

like image 30
RandomGuy Avatar answered Oct 04 '22 21:10

RandomGuy