在javascript中,在客户端访问隐藏变量Vs服务器变量

时间:2021-08-20 15:21:17

I'm evaluating two options of accessing a server side data on client side. Little bit confused about the efficiency or may be you can call it as finding best approach to do it.

我正在评估访问客户端服务器端数据的两个选项。对效率有点困惑,或者你可以称之为找到最佳的方法。

I need to access a server side data may be an integer value in javascript on client side. I know about two options to do it.

我需要访问的服务器端数据可能是javascript客户端的整数值。我知道有两个选择。

  1. Create a public variable or property on server side and set it to javascript variable on client side as below:

    在服务器端创建一个公共变量或属性,并将其设置为客户端javascript变量,如下所示:

    var value = eval(<% =value %>);
    
  2. Create a asp hidden variable and set value in this hidden variable from server side and access it through javascript using document.getElementById().

    从服务器端创建一个asp隐藏变量并在这个隐藏变量中设置值,并使用document.getElementById()通过javascript访问它。

Which is the best approach and what are the pros and cons?

哪一种方法是最好的,优点和缺点是什么?

1 个解决方案

#1


3  

The main difference is that the hidden field will be posted back with the rest of the form fields when the form is submitted. Most people will go this route vs introducing a global javascript variable but if that works for your scenario it is fine.

主要的不同之处在于,当表单提交时,隐藏字段将与其余的表单字段一起返回。大多数人会走这条路,而不是引入一个全局javascript变量,但如果这对您的场景是可行的。

#1


3  

The main difference is that the hidden field will be posted back with the rest of the form fields when the form is submitted. Most people will go this route vs introducing a global javascript variable but if that works for your scenario it is fine.

主要的不同之处在于,当表单提交时,隐藏字段将与其余的表单字段一起返回。大多数人会走这条路,而不是引入一个全局javascript变量,但如果这对您的场景是可行的。