Connecting Tech Pros Worldwide Forums | Help | Site Map

Session Variable

Member
 
Join Date: Sep 2006
Posts: 73
#1: Oct 13 '08
Is't posibble to share session variable within asp and javascript code?

DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#2: Oct 13 '08

re: Session Variable


It is possible to write a session variable to a javascript function by embedding it inside the function using server tags like this:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function test()
  3. {
  4. var a = '<%=Session("MySessionVar")%>';
  5. alert(a);
  6. }
  7. </script>
You can only set a session variable on the server so it must be passed from your javascript to the server through a form or the querystring.

Out of interest, why are you trying to do this?

Hope this helps,

Dr B
Member
 
Join Date: Sep 2006
Posts: 73
#3: Oct 14 '08

re: Session Variable


Thanks it works well... but how to assign to session variable?
Member
 
Join Date: Sep 2006
Posts: 73
#4: Oct 14 '08

re: Session Variable


Actually it's not working..
I want to have the value of session variable that has define by vbcode...
Your code is actually define it back with javascript
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#5: Oct 14 '08

re: Session Variable


No my code is setting a javascript variable to the value of a session variable as defined in your vbscript. Try using the following example and you'll see that the value of the javascript variable 'a' is set to the value of the session variable 'MySessionVar'.

Expand|Select|Wrap|Line Numbers
  1. <%
  2. Session("MySessionVar") = "Hello World!"
  3. %>
  4.  
  5. <script type="text/javascript"> 
  6. function test() 
  7. var a = '<%=Session("MySessionVar")%>'; 
  8. alert(a); 
  9. </script>
  10.  
  11. <body onload="test()">
  12.  
  13. </body>
Does this make sense?

Dr B
Newbie
 
Join Date: Sep 2007
Posts: 2
#6: Oct 16 '08

re: Session Variable


Session.RemoveAll();
string hstring = "<script language='javascript'> window.location.href='logOut.aspx'</script>";
Page.RegisterStartupScript("one", hstring);
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#7: Oct 16 '08

re: Session Variable


banu2001,

Why have you posted this? It looks like ASP.NET C# while this is the Classic ASP Forum and it also looks totally unrelated to the thread.

Dr B
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#8: Oct 17 '08

re: Session Variable


Quote:

Originally Posted by DrBunchman

It is possible to write a session variable to a javascript function by embedding it inside the function using server tags like this:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function test()
  3. {
  4. var a = '<%=Session("MySessionVar")%>';
  5. alert(a);
  6. }
  7. </script>
You can only set a session variable on the server so it must be passed from your javascript to the server through a form or the querystring.

Out of interest, why are you trying to do this?

Hope this helps,

Dr B

Thanks DrBunchman :)
I never thought about doing this and I don't know why.

-Frinny
Reply