Javascript - How to Set Values to Session in JavaScript

JavaScript - How to Set values to Session in Javascript

Not possible to assign session values directly through javascript.

I found alternative ways. Call the code behind function and assign the session values.

Javascript Function:

 function InitializeRequest(path) 
{
// call server side method
PageMethods.SetDownloadPath(path);
}


[System.Web.Services.WebMethod]
public static string SetDownloadPath(string strpath)
{
Page objp = new Page();
objp.Session["strDwnPath"] = strpath;
return strpath;
}

Must enable page methods set to true

<asp:ScriptManager EnablePageMethods="true" ID="MainSM" runat="server" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager>

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>

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/

get and set session variable in javascript

Sounds like you want web storage, specifically sessionStorage, which has excellent support (basically, it's on everything even vaguely recent [even IE8] except Opera Mini).

// On page load (note that it's a string or `undefined`):
var idleTime = parseFloat(sessionStorage.idleTime || "0");

// When updating it (it will automatically be converted to a string):
sessionStorage.idleTime = idleTime += .5;

Having said that, if your goal is to click the logout link after 10 minutes of inactivity, it seems like it could be a bit simpler:

$(document).ready(function() {
var lastActivity = parseInt(sessionStorage.lastActivity || "0") || Date.now();
setInterval(function() {
if (Date.now() - lastActivity > 600000) { // 600000 = 10 minutes in ms
document.getElementById('logoutLink').click();
}
}, 30000);

// In response to the user doing anything (I assume you're setting
// idleTime to 0 when the user does something
$(/*....*/).on(/*...*/, function() {
sessionStorage.lastActivity = lastActivity = Date.now();
});
});

How To Assign Value To Session Using Javascript

The session is a server-side concept; Javascript has no conception of it.

You can make an AJAX service that sets a session value.

However, you should probably use a cookie instead.

You can set cookies in Javascript using this library, among others.

How to set/get session values from javascript

you can't access server session in client side.
but if you want to do some changes in client side according to server session value.
i will give you small idea.it may work for you.

(sorry, I know only java not php,etc.,)

just inside JSP script-let check for the session, create some hidden html element with session value. like this

<% String role=request.getSession().getAttribute("role").toString();%>
<input type="hidden" id="role" value=<%= role ;%> />

And then ,in javascript just get the role from html input element by ID
like this.

var role=document.getElementById("role");

and do you stuff here.

And if you want to set role in session in javascript, it may help you

    <script>
function nameYourFunction()
{
var role="";

if(your condition)
<% request.getSession().setAttribute("your variable","your values"); %>
}

</script>

hope this works.
And call your function, whenever you need.

Want to Set value in Session variable on change event of drop down

 <select>
<option value="<%= session.getAttribute("anything") %>"></option>

</select>

you can insert java code in HTML.



Related Topics



Leave a reply



Submit