user2225394 user2225394 - 9 months ago 46
Javascript Question

How to get a session variable via javascript from an asp server script

I'm new to the javascript world and have a simple test to read session vars in javascript:

my asp file:


my html file:

<script type="text/javascript" src="asp/testSession.asp">
alert("Session ID " + Session("id"));

What am I doing wrong?

Answer Source

All ASP code has to be placed between <% and %> tags to be processed server-side:

alert("Session ID " + <%=Session("id") %>);
                      ^^^ add tags     ^^

Also, you can use <%= as a shortcut to output a variable. It's short for Response.Write.