In code-behind I set Session
with some data.
Session["usedData"] = "sample data";
And the question is how can I get the Session value(in my example; "sample data") in javascript and set Session["usedData"]
with a new value?
Javascript can not directly set session values.
To start PHP sessions, you must use the function session_start() . To set session variables, you will need to apply a global PHP $_SESSION variable . Note: The PHP session_start() function has to be the first thing in your document: all HTML tags come after.
Session is a Server Side object it cann't accessed directly in the Javascript as it calls in the Client Side. So, place your Session value in a hidden filed or pass the value in the Session as a parameter while calling the Script.
Accessing & Assigning the Session Variable using Javascript:
Assigning the ASP.NET Session Variable using Javascript:
<script type="text/javascript"> function SetUserName() { var userName = "Shekhar Shete"; '<%Session["UserName"] = "' + userName + '"; %>'; alert('<%=Session["UserName"] %>'); } </script>
Accessing ASP.NET Session variable using Javascript:
<script type="text/javascript"> function GetUserName() { var username = '<%= Session["UserName"] %>'; alert(username ); } </script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With