How to Access Session Variables and Set Them in JavaScript

How to access Session variables and set them in javascript?

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>

Get Current Session Value in JavaScript?

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.

How to access php SESSION variable in javascript

<?php $session_value=(isset($_SESSION['id']))?$_SESSION['id']:''; ?>
<html>
<head>
<script type="text/javascript">
var myvar='<?php echo $session_value;?>';
</script>
<script type="text/javascript" src="general.js"></script>
</head>
<body>

</body>
</html>

In above code snippet I have initialized global variable myvar with value stored into php session variable into script file before general.js file is referenced in script.It will be accessible in general.js file

how to access session variables from express and nodes js in function.js

Node.js/express run on the server.

jQuery runs on the client.

The only thing that's shared is whatever you've sent to the client.

If you want a variable to be accessible client-side, you need to send it in the response.

If you're giving the server JSON back, just add a field.

res.send({t1: req.session.t1}); // (node.js side)

If you're outputting a template, you can write it in a HTML tag, or directly in a script tag.

Jade example:

script. // .jade file
var t1 = "#{t1}"

And in the server:

res.render('template.jade', {t1: req.session.t1});

Access Session Variable in javascript

I found this answer:

No, session state is server-side.

I then use the following:

I still set the session variable in the controller when the user log in

HttpContext.Session.Add("IsShowStartPopUp", true);

I then created the following in my MVC view:

@if (HttpContext.Current.Session["IsShowStartPopUp"].ToString() == true)
{
HttpContext.Current.Session["IsShowStartPopUp"] = false;
<script type="text/javascript">
window.onload = function () {ShowPopup();}
</script>
};

This solved my issue and if there is any suggestions you are more that welcome to comment.

Thanks

Read session variable in javascript?

Seems you should be able to use getAttribute():

var name = '<%= session.getAttribute("userName") %>';

Though, this depends on Java running through the file to replace the embedded <%= ... %>, which probably won't be the case in separate .js files.

How can I set session variable with JavaScript

Please try it if you want to use session using jquery
First include jquery-1.9.1.js and jquery.session.js

$(document).ready(function(){ $('.button').click(function(){var href = $(this).val();$.session.set("yoursessioname", "storevalue");}) });
alert($.session.get("yoursessioname"));

more detail http://phprocks.letsnurture.com/create-session-with-jquery/

How do I access session variable from php in external javascript

If you want to call php variable in external js file then you have to store value in input.

Main.php

<body>
<input type="hidden" value="$SESSION_['user_id']" class="user_id">
<script src="login.js"></script>
<script src="postad.js"></script> //the script from which I want to get $SESSION_['user_id']

</body>

postad.js

$(function(){
var user_id = $('.user_id').val();
alert(user_id);
});

And if you want to use in same file then you can directly use php echo.

Main.php

<body>
//content..

<script>
var user_id = "<?php echo $SESSION_['user_id'] ?>";
</script>
<script src="login.js"></script>
<script src="postad.js"></script> // console.log(user_id); that file you'll get that id.
</body>


Related Topics



Leave a reply



Submit