简体   繁体   English

我们可以在C#中使用javascript吗?

[英]Can we use javascript in C#?

I want to use Javascript functionality in c#. 我想在c#中使用Javascript功能。 Is it possible to use Javascript in c#? 是否可以在c#中使用Javascript? If yes how? 如果是,怎么办?

Can anyone provide me some guide or sample code? 谁能给我一些指南或示例代码?

Thanx 谢谢

For executing a javascript function on page load use, 为了在页面加载时执行javascript函数,

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显示了一个简单的动态JScript eval,可以从C#调用它,无论您要查找的是什么,我都不确定100%。

You can send JavaScript to the page in a number of methods. 您可以通过多种方法将JavaScript发送到页面。

1) You can put a literal control on your page and set the value to a script. 1)您可以在页面上放置文字控件并将其值设置为脚本。 2) Use the ASP.NET scriptmanager to registerstartupscript . 2)使用ASP.NET脚本管理器注册 startupscript。

This is mostly useless but the JVM(Java Virtual Machine) has rhino a JavaScript engine. 这通常没有用,但是JVM(Java虚拟机)具有犀牛 JavaScript引擎。 This is only mostly useless as IKVM is a Java implementation for .net including 这几乎没有什么用,因为IKVM是.net的Java实现,包括

 * 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 允许您以非常round回的方式使用JavaScript,如下所示: http : //www.codeproject.com/KB/cs/EmbeddingJSCS.aspx

also Jint looks interesting. Jint看起来也很有趣。

No you can't invoke a function written in Javascript in C#. 不,您不能调用用C#用Javascript编写的函数。

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. 当然,您可以在ASPX页内编写C#,该页可以在客户端页上调用Javascript函数,而这可能并不是您想要的。

You can try to use JSC (jscript compiler) to compile your javascript code into a library which can then be referenced by .NET. 您可以尝试使用JSC(jscript编译器)将javascript代码编译到一个库中,然后可由.NET引用。 However, jscript and javascript syntax differ, and you will more than likely be stuck having to make changes. 但是,jscript和javascript语法有所不同,您很有可能不得不进行更改。

I ran into this problem several months ago. 几个月前,我遇到了这个问题。 My solution was to port my javascript code to C# and use that. 我的解决方案是将我的JavaScript代码移植到C#并使用它。

Using the built in javascript debugging tools inside IE makes it pretty easy to do. 使用IE中的内置javascript调试工具可以轻松实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM