简体   繁体   中英

Setting an ASP session variable to Javascript global variable value

How can i set the value of a ASP session variable - Session("CaptchaVal") .
I want to assign its value to a Javascript global variable - var genValue .

In your ASP page create a script block with the variable defined:

<script type='text/javascript'>
  var genValue = '<%=Session("CaptchaVal")%>';
</script>

Of course, that is a simple example.

You need to be careful if you value might contain a single quote.

You should escape the value from the session.

You need to be careful to put that script block prior to any other js code that might attempt to use genValue.

Since Session variables are stored on the server and javascript is purely on the clientside, simply setting a session variable this way will not work. You need to use a request (either get or post) which will be sent to the server and put your setting of the session variable into that request.

Try looking for ajax request with jquery. That way you can send the needed parameters to an asp file where you can retrieve them and set the session, as the asp files are executed on the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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