8

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.

asked Apr 25, 2012 at 12:54
6
  • 4
    .Net is pretty good for text processing. Is there something specific you need that only JavaScript can provide? Commented Apr 25, 2012 at 12:57
  • I'd second @MarceloCantos' comment. What's the advantage of going outside of .Net for text processing? Commented Apr 25, 2012 at 13:00
  • 2
    This seems like a bizarre thing to want to do. What exactly are you trying to accomplish? Commented Apr 25, 2012 at 13:00
  • i need to test JavaScript code that it performs correctly. that is in an automatic way. Commented Apr 25, 2012 at 13:19
  • There are a host of javascript unit testing frameworks out there. Commented Apr 25, 2012 at 22:16

2 Answers 2

13

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.

answered Apr 25, 2012 at 12:57
3
  • any links on how to interact with chrome or ie javascript engines? Commented Apr 25, 2012 at 13:19
  • 1
    it looks like Jint is the best option :) Commented Apr 25, 2012 at 17:45
  • 1
    Cool of you to come back and update this years later. Going to check out ChakraCore now. Thanks! Commented Apr 3, 2018 at 5:17
4

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.

answered Apr 25, 2012 at 13:00
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.