tags:

views:

140

answers:

5

Hi,

I want to use Javascript functionality in c#. Is it possible to use Javascript in c#? If yes how?

Can anyone provide me some guide or sample code?

Thanx

A: 

http://odetocode.com/code/80.aspx shows a simple dynamic JScript eval which can be called from C#, whether this is what you are looking for I am not 100% sure.

tyranid
just a note: jscript and javascript are two different beasts.
Alan
No they are not, they are just variants on ECMAScript and normally when people ask for running 'javascript' in a windows technology they are really referring to JScript. And in my experience people tend to confuse javascript with what ever the web browser actually implements, as from what I can tell the majority of differences between JScript and Javascript are related to scoping and how DOM interaction occurs, which is all about the web browser not the language.
tyranid
While JScript and Javascript are very closely related, JScript.NET is indeed a different beast. Alan is presumably referring to the fact that the article is talking about JScript.NET and not JScript.
Svend
A: 

You can send JavaScript to the page in a number of methods.

1) You can put a literal control on your page and set the value to a script. 2) Use the ASP.NET scriptmanager to registerstartupscript.

Dustin Laine
+1  A: 

For executing a javascript function on page load use,

Page.ClientScript.RegisterStartupScript(Page.GetType(), "jsstring", "urfunction()", true);

For executing on a button click event use,

ScriptManager.RegisterClientScriptBlock(urbuttonId, typeof(button), "jsstring", "urfuction()", true);

Pandiya Chendur
A: 

This is mostly useless but the JVM(Java Virtual Machine) has rhino a JavaScript engine. This is only mostly useless as IKVM is a Java implementation for .net including

* A Java Virtual Machine implemented in .NET
* A .NET implementation of the Java class libraries
* Tools that enable Java and .NET interoperability

allows you to use JavaScript in a very roundabout way as seen here:http://www.codeproject.com/KB/cs/EmbeddingJSCS.aspx

also Jint looks interesting.

Roman A. Taycher
A: 

No you can't invoke a function written in Javascript in C#.

You can, of course write C# inside an ASPX page that can invoke a Javascript function on the client page which is probably not what you are trying to do.

You can try to use JSC (jscript compiler) to compile your javascript code into a library which can then be referenced by .NET. However, jscript and javascript syntax differ, and you will more than likely be stuck having to make changes.

I ran into this problem several months ago. My solution was to port my javascript code to C# and use that.

Using the built in javascript debugging tools inside IE makes it pretty easy to do.

Alan