ANS: Using ASP Variables

C

Craig L

This is a solution followup to a request for help posted on 11/04/03 at
12:51pm titled "Using ASP Variables". Here is how you can make ASP variables
available for use in Javascript code. I hope you find this info as valuable
as I did. Thanks to everyone who helped me get to this solution with their
wonderful ideas.

1) Create session objects for the variables (this example provides strings,
but they could come out of a database):

<!-- Get data for Session Objects -->
<%
session("Firstname")="Mary"
session("Lastname")="Thomas"
%>

2) Create string values:

<!-- Set string values -->
<%
sFirstname=session("Firstname")
sLastname=session("Lastname")
%>

3) Use response.write to create the Javascript code:

<!-- Create Javascript variables -->
<%
response.write "<SCRIPT Language=" & chr(34) & "JavaScript" & chr(34) & ">"
response.write "var strFirstname=" & chr(34) & sFirstname & chr(34) &";"
response.write "var strLastname=" & chr(34) & sLastname & chr(34) &";"
response.write "</SCRIPT>"
%>

4) Now, strFirstname and strLastname values are avail for us in other
Javascript code.

Since I have the ASP to JS issue solved, I will work on the JS to ASP issue
soon. Thanks.

Craig

(e-mail address removed) (remove SPAM before emailing.)
 
M

Martin Honnen

Craig said:
This is a solution followup to a request for help posted on 11/04/03 at
12:51pm titled "Using ASP Variables". Here is how you can make ASP variables
available for use in Javascript code. I hope you find this info as valuable
as I did. Thanks to everyone who helped me get to this solution with their
wonderful ideas.

1) Create session objects for the variables (this example provides strings,
but they could come out of a database):

<!-- Get data for Session Objects -->
<%
session("Firstname")="Mary"
session("Lastname")="Thomas"
%>

2) Create string values:

<!-- Set string values -->
<%
sFirstname=session("Firstname")
sLastname=session("Lastname")
%>

3) Use response.write to create the Javascript code:

<!-- Create Javascript variables -->
<%
response.write "<SCRIPT Language=" & chr(34) & "JavaScript" & chr(34) & ">"
response.write "var strFirstname=" & chr(34) & sFirstname & chr(34) &";"
response.write "var strLastname=" & chr(34) & sLastname & chr(34) &";"
response.write "</SCRIPT>"
%>

4) Now, strFirstname and strLastname values are avail for us in other
Javascript code.

Since I have the ASP to JS issue solved, I will work on the JS to ASP issue
soon. Thanks.

That is far from being solved, imagine the ASP string value has double
quotes in it, then your approach breaks and you get a client side script
error. Or imagine the ASP string value contains a line break, your code
would then Response.Write that line break and again you get a client
side script error. So do some work on escaping certain characters first.
Not to speak about variables of other types, the subject speaks about
ASP variables so your code should be able to deal with numbers,
booleans, maybe dates.
 
E

Evertjan.

Martin Honnen wrote on 10 nov 2003 in comp.lang.javascript:
Not to speak about variables of other types, the subject speaks about
ASP variables so your code should be able to deal with numbers,
booleans, maybe dates.

Last night the OP could have had a date with a ASP variable.

;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top