How to get a session variable via javascript from an asp server script
13,251
Solution 1
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
.
Solution 2
You cannot mix javascript and asp in the way you did it. Javascript is executed locally while asp is compiled by the server and then send to your browser.
When the page reaches your browser, only the product of the asp compilation remains. In order to use the value or print it, you should do the following :
<html>
<head></head>
<body>
<script type="text/javascript" src="asp/testSession.asp">
alert("Session ID " + <%=Session("id")%>);
</script>
</body>
Author by
user2225394
Updated on June 04, 2022Comments
-
user2225394 almost 2 years
I'm new to the javascript world and have a simple test to read session vars in javascript:
my asp file:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <% Session("id")=1234 Session("code")="ZZ" %>
my html file:
<html> <head></head> <body> <script type="text/javascript" src="asp/testSession.asp"> alert("Session ID " + Session("id")); </script> </body>
What am I doing wrong?
-
cchamberlain over 8 yearsNot always the case. Classic ASP can process server side "JScript". If you use a script tag with language "JScript" and runat="server", the script will be run server side and can interop with server-side VBScript code. See support.microsoft.com/en-us/kb/288965