Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to assign a session variable to a variable in javascript

Status
Not open for further replies.

lucyc

Programmer
Mar 1, 2002
62
US
Hi , I have a page that I want to assign a session variable that I created from the previous page to a variable called strAgency. I use the following code :


<SCRIPT LANGUAGE=&quot;JAVASCRIPT&quot;>
var strAgency = <%=Session(&quot;Agency&quot;)%>;
alert (&quot;agency = &quot; + strAgency);
</SCRIPT>

But for some reason, the strAgency changed to 19 ( i have no idea where is the value coming from.) I use the response.write to check the value in session(&quot;agency&quot;) and it shows the correct value (023) .

<%
'Response.Write &quot;Agency : &quot; & session(&quot;agency&quot;)
%>

Can someone tell me why????? Thanks.
 
try
<SCRIPT LANGUAGE=&quot;JAVASCRIPT&quot;>
var strAgency = &quot;<%=Session(&quot;Agency&quot;)%>&quot;;
alert (&quot;agency = &quot; + strAgency);
</SCRIPT>

quotes are needed A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
also i noticed there is a case difference in the session variable
you have
<%=Session(&quot;Agency&quot;)%>
and then reference it like this
& session(&quot;agency&quot;)

which is correct? this will make a difference A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Hi onpnt,

Thanks for you fast response. Now I am getting the correct value. But if I do this, I am not getting the correct value. What is wrong ???

function FormChanged()
{
var strAgency = &quot;<%=Session(&quot;Agency&quot;)%>&quot;;
alert(&quot; stragency = &quot; + (strAgency.substr,2,1));
If ((strAgency.substr,2,1)==&quot;7&quot;)
{...............
Thanks for your help.
 
syntax for the substr function is
String.substr(a, b)
so for this
(strAgency.substr(2,1))

I would also think that the 7 should not be in &quot; &quot; due to it being a int and not a string. I may be mistaken on that though A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Hi onpnt,

Thank you very much for helping me. Now I am getting the correct value. I need to test the value to see if it is a int or a string. Thanks again. Have a good day.
 
Hello onpnt,

You are right, it is a int. It returns false when I use isNaN function. Now I have another problem. I keep getting the &quot;object expected&quot; error message on the last line of the following code. What did I do wrong????? Thanks.
function FormChanged()
{
var strAgency = &quot;<%=Session(&quot;Agency&quot;)%>&quot;;
alert (&quot;Agency is : &quot; + strAgency.substr(0,1));
document.write(isNaN(strAgency.substr(0,1)))
If ((strAgency.substr(0,1)) == 7) // this line
 
javascript is case sensitive. try using a lower case if. that should fix it A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Hey onpnt,

Thanks very very much. You are the best.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top