I am using an external javascript file for my asp.net project. Now i want to get the session value in that javascript. How can i get the session value in that javascript file?
Thanks in advance..
Session is a variable on the backend server side, while JS is a previous script. There is no ready-made method in JS to get the value of Session, and it needs to be obtained through the server language. For example, java can be used to get the value of Session and assign it to JS variable.
getItem(key): This method is used to get the value that is stored into the Session Storage. It takes a key and returns the value. removeItem(key): This method is used to remove a particular value from the Session Storage basis of the key. clear(): This method is used to clear the Session Storage.
You can check whether a variable has been set in a user's session using the function isset(), as you would a normal variable. Because the $_SESSION superglobal is only initialised once session_start() has been called, you need to call session_start() before using isset() on a session variable.
Session storage is a popular choice when it comes to storing data on a browser. It enables developers to save and retrieve different values. Unlike local storage, session storage only keeps data for a particular session. The data is cleared once the user closes the browser window.
I tried following with ASP.NET MVC 5, its works for me
var sessionData = "@Session["SessionName"]";
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