ANS: Using ASP Variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig L

    ANS: Using ASP Variables

    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("Firstn ame")="Mary"
    session("Lastna me")="Thomas"
    %>

    2) Create string values:

    <!-- Set string values -->
    <%
    sFirstname=sess ion("Firstname" )
    sLastname=sessi on("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

    w1zard10@hotSPA Mmail.com (remove SPAM before emailing.)



  • Martin Honnen

    #2
    Re: ANS: Using ASP Variables



    Craig L wrote:
    [color=blue]
    > 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("Firstn ame")="Mary"
    > session("Lastna me")="Thomas"
    > %>
    >
    > 2) Create string values:
    >
    > <!-- Set string values -->
    > <%
    > sFirstname=sess ion("Firstname" )
    > sLastname=sessi on("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.[/color]

    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.

    --

    Martin Honnen


    Comment

    • Evertjan.

      #3
      Re: ANS: Using ASP Variables

      Martin Honnen wrote on 10 nov 2003 in comp.lang.javas cript:
      [color=blue]
      > 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.
      >[/color]

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

      ;-)

      --
      Evertjan.
      The Netherlands.
      (Please change the x'es to dots in my emailaddress)

      Comment

      Working...