7

I'd like to host Javascript in my C# program. I would like to allow users to write custom Javascript code, and have my C# program run their functions, as well as allow the users to use my framework code.

Is it possible to do this? If so, how?

Edit: To be clear, I am not using ASP.NET for this project.

Erik Forbes
  • 35,357
  • 27
  • 98
  • 122

6 Answers6

3

you can also use a webbrowser control to host the javascript in a html document, to interact between the two you would make a COM visible class and set an instance of it to the ObjectForScripting property.

Any public members of the 'external' class are now accessible through window.external in javascript. Also, from the managed code side you can use the InvokeScript method of the document to call any javascript functions.

This way it is easy to pass complex objects between the two.

I used this idea quite a bit in this Google Earth Api application. http://fraserchapman.blogspot.com/2008/08/google-earth-plug-in-and-c.html

Fraser
  • 15,275
  • 8
  • 53
  • 104
  • Ah, excellent! In my previous googling I'd found people suggesting the webbrowser control approach, but with no explanation offered I figured the suggestions were outside my use case. Thanks =) – Erik Forbes Mar 26 '09 at 22:04
  • If you check this example as well you can see some more complex interaction, including the ability to inject javascript at runtime... See the IHTMLScriptElement.cs in the source. http://code.google.com/p/winforms-geplugin-control-library/ – Fraser – Fraser Mar 26 '09 at 23:50
2

I'd highly recommend you use the Microsoft Dynamic Langauage Runtime (DLR). It's purpose in life is to facilitate scripting in a .Net environment. The DLR is not script specific so over time more script languages will be built on top of it but at the moment the ones I know about are IronRuby, IronPython and JScript.

sipsorcery
  • 30,273
  • 24
  • 104
  • 155
  • I may go this route however I like JavaScript as a language. It would be really nice if there were a DLR implementation of JavaScript but to date I haven't seen anything in that space. – Erik Forbes Dec 09 '09 at 01:26
  • There used to be, the early samples for the Microsoft.Scripting library had JScript samples. I have read that it's no longer being maintained though. – sipsorcery Dec 09 '09 at 09:55
  • Yes you would use the DLR and the Type dynamic - but you would still have to first host the JS in a browser control and create a COM visible class. Once that is done then the DLR makes the interop a breeze as you can use dot syntax, etc, and not have to worry with reflection and invocation, However, the DLR won't run the JS for you, it and the dynamic Type will just let you IO with it much more easily. – Fraser Jun 08 '11 at 19:01
1

Spidermonkey .NET Is probably what you are looking for.

I'm not sure how developed it is. Buts its basically a port of Mozilla's Spidermonkey Javascript parser engine which I like a lot.

Or you can evaluate it directly

bdd
  • 3,436
  • 5
  • 31
  • 43
1

The way to do this used to be the Windows Script Host, which permitted you to host either JSCript or VBScript.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
0

You can certainly run Javascript natively from C# - see this article for a starter. Having the Javascript access your framework could be tricker, though. Perhaps Script# could help?

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • Script# would help write the JS but I don't think it would help him call .NET code from JS. – bdd Mar 26 '09 at 21:57
0

There are HTML components where you can embed JavaScript. For the other way the proper thing is an ASP.NET web service. Perhaps with a smart client you can combine both.

Olav
  • 1,758
  • 4
  • 27
  • 49