0

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

3
  • 6
    Could you elaborate on what you mean by "use javascript functionality in C#"? (i.e. I need to add scripting to my application) Commented Mar 22, 2010 at 6:13
  • 1
    Do you mean us javascript client side in an .aspx page, or execute javascript on the server/desktop from C#? Commented Mar 22, 2010 at 6:14
  • 2
    Perhaps you mean "How can I use Javascript as a .Net language?"? Commented Mar 22, 2010 at 6:16

5 Answers 5

1

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);

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

just a note: jscript and javascript are two different beasts.
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.
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.
0

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.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

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.