如何在 C# 中执行 javascript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5934736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:51:07  来源:igfitidea点击:

How to execute javascript in C#?

c#javascript

提问by Domi.Z

Possible Duplicate:
Embedding JavaScript engine into .NET (C#)

可能的重复:将
JavaScript 引擎嵌入 .NET (C#)

I have a script like this:

我有一个这样的脚本:

var myObject=new Object();
myObject="Hello";

If I refer the script in browser, I can make use of myObject just like below:

如果我在浏览器中引用脚本,我可以像下面一样使用 myObject:

<script src="myObject.js" type="text/javascript">
<script type="text/javascript">
    alert(myObject);
</script>

Can I execute the script in C# without browser? It should be like below:

我可以在没有浏览器的情况下在 C# 中执行脚本吗?它应该如下所示:

//These are C# Code
string result = ????????

回答by Devjosh

not getting your question properly i think you are using the term browser in place of html if it' so then there is a method Page.RegisterClientScriptBlock();

没有正确回答你的问题我认为你使用术语浏览器代替 html 如果它'所以那么有一个方法 Page.RegisterClientScriptBlock();

for more detail read on http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerclientscriptblock.aspx

有关更多详细信息,请阅读 http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerclientscriptblock.aspx

you can use this in whicever event you want to run the script .

您可以在要运行脚本的任何事件中使用它。

not that i assumed browse==HTML code

不是我假设浏览==HTML 代码

回答by Ahmed Magdy

Have you checked Page.ClientScript.RegisterStartupScript()method http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx

您是否检查过Page.ClientScript.RegisterStartupScript()方法http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx

the page have an examples

该页面有一个示例

回答by Flavio Oliveira

Well,

好,

i think you're a bit confused. try first to understand the difference between Client side-script and Server-side code.

我觉得你有点困惑。首先尝试了解客户端脚本和服务器端代码之间的区别。

Basically server-side produces the rendered html for client-side(your browser).

基本上服务器端为客户端(您的浏览器)生成呈现的 html。

It is not possible to create objects like your example, you have to use JSON to get objects from server.

无法像您的示例那样创建对象,您必须使用 JSON 从服务器获取对象。

but if you what to get the object value on c#, you must send it to the server, on a and parse it on your server side

但是如果你在 c# 上获取对象值,你必须将它发送到服务器,并在你的服务器端解析它