Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute JavaScript from within a C# assembly

Tags:

I'd like to execute JavaScript code from within a C# assembly and have the results of the JavaScript code returned to the calling C# code.

It's easier to define things that I'm not trying to do:

  • I'm not trying to call a JavaScript function on a web page from my code behind.

  • I'm not trying to load a WebBrowser control.

  • I don't want to have the JavaScript perform an AJAX call to a server.

What I want to do is write unit tests in JavaScript and have then unit tests output JSON, even plain text would be fine. Then I want to have a generic C# class/executible that can load the file containing the JS, run the JS unit tests, scrap/load the results, and return a pass/fail with details during a post-build task.

I think it's possible using the old ActiveX ScriptControl, but it seems like there ought to be a .NET way to do this without using SilverLight, the DLR, or anything else that hasn't shipped yet. Anyone have any ideas?

update: From Brad Abrams blog

namespace Microsoft.JScript.Vsa
{
    [Obsolete("There is no replacement for this feature. " +
              "Please see the ICodeCompiler documentation for additional help. " +
              "http://go.microsoft.com/fwlink/?linkid=14202")]

Clarification: We have unit tests for our JavaScript functions that are written in JavaScript using the JSUnit framework. Right now during our build process, we have to manually load a web page and click a button to ensure that all of the JavaScript unit tests pass. I'd like to be able to execute the tests during the post-build process when our automated C# unit tests are run and report the success/failure alongside of out C# unit tests and use them as an indicator as to whether or not the build is broken.

like image 519
ScottKoon Avatar asked Sep 15 '08 22:09

ScottKoon


People also ask

Can you call a JavaScript function from C#?

For calling C# method from JavaScript you can use ScriptManager or jQuery . I personally use jQuery . You need to decorate the method that you want to call from JavaScript with WebMethod attribute. For more information regarding calling C# method (called PageMethod ) from jQuery you can refer to Dave Ward's post.

Can C++ call JavaScript?

Using emscripten_run_script() void emscripten_run_script (const char *script) is the most direct way of calling JavaScript from C/C++. It effectively runs the code using eval() which is a JavaScript function that evaluates code represented as a string.

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").

How do I call a JavaScript function with parameters from code behind C#?

In order to call the JavaScript function with parameter from Code Behind, one has to make use of the RegisterStartupScript method of the ClientScript class in ASP.Net using C# and VB.Net. The following HTML Markup consists of an ASP.Net Button and a Label control.


1 Answers

The code should be pretty self explanitory, so I'll just post that.

<add assembly="Microsoft.Vsa, Version=8.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies>

using Microsoft.JScript;

public class MyClass {

    public static Microsoft.JScript.Vsa.VsaEngine Engine = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();

    public static object EvaluateScript(string script)
    {
        object Result = null;
        try
        {
            Result = Microsoft.JScript.Eval.JScriptEvaluate(JScript, Engine);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }

        return Result;
    }

    public void MyMethod() {
        string myscript = ...;
        object myresult = EvaluateScript(myscript);
    }
}
like image 85
scubabbl Avatar answered Sep 25 '22 17:09

scubabbl