I have a scenario where I open my web application in a browser but in two separate tabs.
In one tab I signed out from the application and as a result the all session val
<script type="text/javascript">
$(document).ready(function(){
var userId=<%: Session["userId"] %>;
alert(userId);
})
</script>
**Get the current session value in jQuery**
The session is a server side thing, you cannot access it using jQuery.
You can write an Http handler (that will share the sessionid if any) and return the value from there using $.ajax
.
i tested this method and it worked for me. hope its useful.
assuming that you have a file named index.php
and when the user logs in, you store the username in a php session; ex. $_SESSION['username'];
you can do something like this in you index.php file
<script type='text/javascript'>
var userName = "<?php echo $_SESSION['username'] ?>"; //dont forget to place the PHP code block inside the quotation
</script>
now you can access the variable userId in another script block placed in the index.php file, or even in a .js file linked to the index.php
ex. in the index.js file
$(document).ready(function(){
alert(userId);
})
it could be very useful, since it enables us to use the username and other user data stoerd as cookies in ajax queries, for updating database tables and such.
if you dont want to use a javascript variable to contain the info, you can use an input with "type='hidden';
ex. in your index.php file, write:
<?php echo "<input type='hidden' id='username' value='".$_SESSION['username']."'/>";
?>
however, this way the user can see the hidden input if they ask the browser to show the source code of the page. in the source view, the hidden input is visible with its content. you may find that undesireble.
I am using C# mvc Application. I have created a session in the Controller class like this
string description = accessDB.LookupSomeString(key, ImpDate);
this.Session["description"] = description;
in the View, We access the session value and store it in a variable, like this (this is within an $.ajax, but I think it should work within any jquery)
var Sessiondescription = '@Session["description"]';
alert(Sessiondescription);
The way i resolved was, i have written a function in controller and accessed it via ajax on jquery click event
First of all i want to thank @Stefano Altieri for giving me an idea of how to implement the above scenario,you are absolutely right we cannot access current session value from clientside when the session expires.
Also i would like to say that proper reading of question will help us to answer the question carefully.