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
Most Efficient Way to Compare Two Ienumerables (Or Lists) in Linq
How to Upload a Excel File to SQL Database Table Using C# Windows Form Application
Asp.Net Core API Post Parameter Is Always Null
Microsoft Azure: How to Create Sub Directory in a Blob Container
Convert Dictionary Values into Array
How to Automatically Increment Numbers in C#
Filling a Character Array With Characters from a String
How to Map to a Dictionary Object from Database Results Using Dapper Dot Net
How to Get an Specific Header Value from the Httpresponsemessage
Repaired Records:Cell Information from Worksheet Created from Scratch
How to Change a Image on a Button Using Windows Forms
Get File Name from Byte Array or Stream
C#: How to Force Trailing Zero in Numeric Format String
How to Convert a String Containing Am/Pm to Datetime
How to Directly Print a Report Without Going Through Crystal Reports Viewer
Regular Expression for Anything But an Empty String
How to Add Double Quotes to a String That Is Inside a Variable