[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

Set Session variable in Javascript

Hi i want to set the javascript value in session
so that i can acess that value from any other page
something like the following..........
<script type="text/javascript" >
    function redirectmenu(id) {
            Session('Menuid') = id;
            location.href = 'Home.aspx";
           }

</script>
0
sathyanreddy
Asked:
sathyanreddy
2 Solutions
 
thecodistCommented:
How about using cookies - there are plenty of sites out there that demonstrate using cookies from Javascript. The first one I ran across was: http://www.quirksmode.org/js/cookies.html. Using the functions from this site please see the attached code for three files. Save them as instructed and then run Page1.html. This provides a simple demonstration. If you really have your heart set on the syntax listed above, you could wrap this in a class of your own.

/* File 1 - Save as Cookies.js */
/* Functions from http://www.quirksmode.org/js/cookies.html */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
 
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
 
function eraseCookie(name) {
	createCookie(name,"",-1);
}
 
/* File 2 - Save as Page1.html */
<html>
<head>
<title>Javascript Session Cookie - Page 1</title>
<script language="javascript" type="text/javascript" src="Cookies.js"></script>
<script language="javascript" type="text/javascript">
function SaveSessionVal(cookieName, cookieValue, sessionDaysToSave) {
	createCookie(cookieName,cookieValue,sessionDaysToSave)
}
</script>
</head>
<body>
<form>
<input type="button" value="Save Session Cookie" onclick="SaveSessionVal('MenuID', '12', 7)">
<a href="Page2.html">Next Page</a>
</form>
</body>
</html>
 
/* File 3 - Save as Page2.html */
<html>
<head>
<title>Javascript Session Cookie - Page 2</title>
<script language="javascript" type="text/javascript" src="Cookies.js"></script>
<script language="javascript" type="text/javascript">
function GetSessionVal(cookieName) {
	var sessionVal = readCookie(cookieName);
	alert(sessionVal);
}
</script>
</head>
<body>
<form>
<input type="button" value="Get Session Cookie" onclick="GetSessionVal('MenuID')">
</form>
</body>
</html>

Open in new window

0
 
CodebotCommented:
No, sessions can't be accessed by javascript directly. It's stored on server, while javascript is running on client.
But can be done indirectly, e.g., store it in a hidden filed, submit it to the server, and retrieve and assign the values in hidden filed to session.
or you can use ajax to get session value.

Hope it helps.

http://www.velocityreviews.com/forums/t120538-setting-session-variable-from-javascript.html
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now