How to access session variables and set them in javascript?

In encoding, I installed Session with some data.

 Session["usedData"] = "sample data"; 

And the question is, how can I get the Session value (in my example, "data samples") in javascript and set Session["usedData"] with a new value?

+55
javascript session
Mar 20 '13 at 9:17
source share
12 answers

Access and assign a session variable using Javascript:

Assigning an ASP.NET Session Variable Using Javascript:

  <script type="text/javascript"> function SetUserName() { var userName = "Shekhar Shete"; '<%Session["UserName"] = "' + userName + '"; %>'; alert('<%=Session["UserName"] %>'); } </script> 

Accessing an ASP.NET Session Variable Using Javascript:

 <script type="text/javascript"> function GetUserName() { var username = '<%= Session["UserName"] %>'; alert(username ); } </script> 
+50
Nov 27 '13 at 7:38
source share

You cannot access Session directly in JavaScript.

You can create a hidden field and pass it to your page, and then use JavaScript to retrieve the object through document.getElementById

+14
Mar 20 '13 at 9:18
source share

try it

 var sessionValue = '<%=Session["usedData"]%>' 
+10
Mar 20 '13 at 9:18
source share

Javascript cannot set session values ​​directly. To set session values ​​from javascript, I make an ajax call as follows.

Check online

In ASPx or html file

  <script type="text/javascript"> $(function(){ //Getting values from session and saving in javascript variable. // But this will be executed only at document.ready. var firstName = '<%= Session["FirstName"] ?? "" %>'; var lastName = '<%= Session["LastName"] ?? "" %>'; $("#FirstName").val(firstName); $("#LastName").val(lastName); $('Button').click(function(){ //Posting values to save in session $.post(document.URL+'?mode=ajax', {'FirstName':$("#FirstName").val(), 'LastName':$("#LastName").val() } ); }); }); 

Server side

 protected void Page_Load(object sender, EventArgs e) { if(Request.QueryString["mode"] != null && Request.QueryString["mode"] == "ajax") { //Saving the variables in session. Variables are posted by ajax. Session["FirstName"] = Request.Form["FirstName"] ?? ""; Session["LastName"] = Request.Form["LastName"] ?? ""; } } 

To get session values, as Shehar and Rajiv said

 var firstName = '<%= Session["FirstName"] ?? "" %>'; 

Hope this helps.

+8
Sep 23 '14 at 14:16
source share

Assuming you mean "client-side JavaScript", then you cannot, at least not directly.

Session data is stored on the server, so the client code cannot see it without communication with the server.

To access it, you must make an HTTP request and have a program on the server side, modify / read and return data.

+5
Mar 20 '13 at 9:19
source share

Assign a value to a hidden field in a code file. Get this value in your javascript, like regular HTML control.

+3
Mar 20 '13 at 9:19
source share

You cannot set session session variables with Javascript . If you want to do this, you need to create an AJAX POST to update it on the server , although if choosing a car is a major event, it might just be easier than POST .

+2
Apr 11 '16 at 11:38 on
source share

I searched for a solution to this problem until I found a very simple solution for accessing session variables, assuming you are using a .php file on the server side. Hope he answers part of the question:

access session value

 <script type="text/javascript"> var value = <?php echo $_SESSION['key']; ?>; console.log(value); </script> 

Edit: the best example below, which you can try on phpfiddle.org, under the "Code Space" tab

 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <?php $_SESSION['key'] = 10; $i = $_SESSION['key']; ?> </head> <body> <p id="affichage">hello</p> <script type="text/javascript"> var value = <?php echo $i; ?>; $("#affichage").html(value); </script> </body> </html> 

set session value

pass it the variable you may want. eg,

 $you = 13; $_SESSION['user_id'] = $you; 

This should work, "did not check."

+1
Nov 26 '15 at 21:50
source share

You can also set a variable in a property and call it from js:

On the server side:

 Protected ReadOnly Property wasFieldEditedStatus() As Boolean Get Return If((wasFieldEdited), "true", "false") End Get End Property 

And then in javascript:

 alert("The wasFieldEdited Value: <%= wasFieldEditedStatus %>" ); 
+1
Feb 03 '16 at 19:45
source share

first create a method in the code to set up the session:

  [System.Web.Services.WebMethod] public static void SetSession(int id) { Page objp = new Page(); objp.Session["IdBalanceSheet"] = id; } 

then call it from the client side:

 function ChangeSession(values) { PageMethods.SetSession(values); } 

you must set EnablePageMethods to true:

 <asp:ScriptManager EnablePageMethods="true" ID="MainSM" runat="server" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager> 
+1
Jul 29 '16 at 8:02
source share

This is a cheat, but you can do it, send the value on the side to the side as a parameter

 <script> var myVar = "hello" window.location.href = window.location.href.replace(/[\?#].*|$/, "?param=" + myVar); //Send the variable to the server side </script> 

And from the server side, find the parameter

 string myVar = Request.QueryString["param"]; Session["SessionName"] = myVar; 

hope this helps

0
Jul 03 '17 at 10:23
source share

If you want to read the session value in javascript. This code will help you.

<script type='text/javascript'> var userID='@Session["userID"]'; </script>

0
Nov 15 '17 at 8:26
source share



All Articles