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>
sathyanreddyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JScript

From novice to tech pro — start learning today.