简体   繁体   English

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

[英]Hidden variable Vs Server variable accessing on client side in javascript

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. 我需要访问服务器端数据可能是客户端javascript中的整数值。 I know about two options to do it. 我知道有两个选择。

  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? 哪种方法最好,有哪些优点和缺点?

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变量,但是如果这对你的场景有用,那就没关系。

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

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