在客户端覆盖runat服务器JavaScript代码

时间:2021-08-22 17:59:25

Let's say I have a script in an ASP.NET page which runs at client side:

假设我在客户端运行的ASP.NET页面中有一个脚本:

<script runat="server">function foo(){
//Some javascript code
}
</script>

My question is, can I(or anyone viewing this site) override this function foo from client side to:

我的问题是,我可以(或任何查看此站点的人)从客户端覆盖此函数foo:

function foo(){
 //my JavaScript code
}

such that it will run at server.

这样它将在服务器上运行。

2 个解决方案

#1


0  

I would say no, as that script (C#, I assume) will run at server, but why do you have that declared in the asp.net page?

我会说不,因为那个脚本(C#,我假设)将在服务器上运行,但为什么你在asp.net页面中声明了?

Wouldn't it be better to have it in container (class for example)? (could be a code-behind file for classic ASP.NET WebForms, in a Model or Controller for MVC)

将它放在容器(例如类)中会不会更好? (可能是经典ASP.NET WebForms的代码隐藏文件,在MVC的模型或控制器中)

Take a look at https://msdn.microsoft.com/en-us/library/f0111sbh(v=vs.100).ASPX

看看https://msdn.microsoft.com/en-us/library/f0111sbh(v=vs.100).ASPX

#2


0  

The attribute runat="server" simply instructs ASP.NET to pre-parse that element (and its child elements if any) on the server, before returning the result to the client browser.

runat =“server”属性只是指示ASP.NET在将结果返回给客户端浏览器之前预先解析服务器上的该元素(及其子元素,如果有的话)。

Once you reach there (the client browser):

到达那里后(客户端浏览器):

  1. You can override the result (e.g. using your own client-side function)
  2. 您可以覆盖结果(例如,使用您自己的客户端功能)

  3. You can't parse your own code on the server again.
  4. 您无法再次在服务器上解析自己的代码。

Hope this makes sense.

希望这是有道理的。

P.S. I know it is a question from 2016

附:我知道这是2016年的一个问题

#1


0  

I would say no, as that script (C#, I assume) will run at server, but why do you have that declared in the asp.net page?

我会说不,因为那个脚本(C#,我假设)将在服务器上运行,但为什么你在asp.net页面中声明了?

Wouldn't it be better to have it in container (class for example)? (could be a code-behind file for classic ASP.NET WebForms, in a Model or Controller for MVC)

将它放在容器(例如类)中会不会更好? (可能是经典ASP.NET WebForms的代码隐藏文件,在MVC的模型或控制器中)

Take a look at https://msdn.microsoft.com/en-us/library/f0111sbh(v=vs.100).ASPX

看看https://msdn.microsoft.com/en-us/library/f0111sbh(v=vs.100).ASPX

#2


0  

The attribute runat="server" simply instructs ASP.NET to pre-parse that element (and its child elements if any) on the server, before returning the result to the client browser.

runat =“server”属性只是指示ASP.NET在将结果返回给客户端浏览器之前预先解析服务器上的该元素(及其子元素,如果有的话)。

Once you reach there (the client browser):

到达那里后(客户端浏览器):

  1. You can override the result (e.g. using your own client-side function)
  2. 您可以覆盖结果(例如,使用您自己的客户端功能)

  3. You can't parse your own code on the server again.
  4. 您无法再次在服务器上解析自己的代码。

Hope this makes sense.

希望这是有道理的。

P.S. I know it is a question from 2016

附:我知道这是2016年的一个问题