i want to be able to execute JavaScript on the server side which is running the .net environment.
so how is it possible to do so?
mainly i need text processing functions, i will input a string and get a string returned from JavaScript code.
no window interaction is needed.
-
4.Net is pretty good for text processing. Is there something specific you need that only JavaScript can provide?Marcelo Cantos– Marcelo Cantos2012年04月25日 12:57:40 +00:00Commented Apr 25, 2012 at 12:57
-
I'd second @MarceloCantos' comment. What's the advantage of going outside of .Net for text processing?KP.– KP.2012年04月25日 13:00:08 +00:00Commented Apr 25, 2012 at 13:00
-
2This seems like a bizarre thing to want to do. What exactly are you trying to accomplish?asawyer– asawyer2012年04月25日 13:00:26 +00:00Commented Apr 25, 2012 at 13:00
-
i need to test JavaScript code that it performs correctly. that is in an automatic way.Karim– Karim2012年04月25日 13:19:03 +00:00Commented Apr 25, 2012 at 13:19
-
There are a host of javascript unit testing frameworks out there.asawyer– asawyer2012年04月25日 22:16:57 +00:00Commented Apr 25, 2012 at 22:16
2 Answers 2
Yes, there are several JS engines which you can use. Jurassic, Jint and IronJS are .NET-based, but you can also interface to others such as the V8 from the Chrome browser or the ActiveScript from IE.
EDIT: Five years later the JS engines native to .NET are somewhat lagging behind (none supports ES6 yet, and IronJS seems abandoned), but we now also have the open-source ChakraCore which is not very hard to integrate and use in .NET with one of the readily available wrappers.
Also, the JavaScriptEngineSwitcher allows using almost any of the existing JS engines from within .NET code through a common interface, so that switching engines does not need changing code.
-
any links on how to interact with chrome or ie javascript engines?Karim– Karim2012年04月25日 13:19:53 +00:00Commented Apr 25, 2012 at 13:19
-
1it looks like Jint is the best option :)Karim– Karim2012年04月25日 17:45:02 +00:00Commented Apr 25, 2012 at 17:45
-
1Cool of you to come back and update this years later. Going to check out ChakraCore now. Thanks!ChevCast– ChevCast2018年04月03日 05:17:47 +00:00Commented Apr 3, 2018 at 5:17
You can write a JScript.Net file and compile it into an assembly with jsc
, then just use that assembly like any other.
Example:
package Your.Desired.Package.Name {
Class SomeClassName {
public static function doSomething(foo) }
var bar;
// Fairly normal JavaScript code here
if (foo.match(/sometest/)) {
// Do something
}
else {
// Do something else
}
}
}
}
Other than the package
and class
structures, the code in JScript.Net is essentially JavaScript. You even have eval
if you want to be evil. :-)
You then compile that into an assembly like this:
jsc /target:library TheFileName.js
...and it produces and assembly in TheFileName.dll
.