开发者

Can we use javascript in C#?

开发者 https://www.devze.com 2022-12-24 11:23 出处:网络
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?开发者_C百科

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?开发者_C百科

Thanx


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


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号