Legacy Guidance:This article preserves historical web development content. For modern .NET 8+ best practices, visit our Tutorials section.
Understanding Where Code Executes
When using ASP, it's important to understand that ASP code exists only on the server. Code surrounded by <% and %> delimiters gets processed completely on the server. The client never accesses this code. If you've created web pages before, you're likely familiar with client-side scripting, which works differently.
What is Client-Side Scripting?
Client-side scripting consists of programmatic code in an HTML file that runs in the browser. This code uses the <script> HTML tag. JavaScript is the most common client-side scripting language because Netscape Navigator only supports JavaScript for client-side operations.
Here's a simple example of a static HTML page with client-side scripting:
Client-Side Scripting Example
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
alert("Hello world");
//-->
</SCRIPT>
</HEAD>
<BODY>
Welcome to my web page!
</BODY>
</HTML>
The Browser Receives Only HTML
When you save the code above as clientside.htm, the entire file gets sent to the browser when the client requests the page. The browser displays a message box when it reaches the alert method during rendering. The web server treats client-side scripting as regular HTML code.
You can include client-side scripting code in an ASP page because the web server sees client-side scripting as plain HTML. The server passes it through unchanged to the browser.
Server-Side Scripts
Server-side scripts are scripts that execute on the web server. These scripts get processed completely, and their output is sent to the client. The client never sees the actual script code.
Server-Side Scripting Example
<%@ Language="VBScript" %>
<HTML>
<HEAD>
<TITLE>Server-Side Example</TITLE>
</HEAD>
<BODY>
<%
' This code runs on the server
Dim userName
userName = Request.Form("name")
If userName <> "" Then
Response.Write "<h1>Welcome, " & userName & "!</h1>"
Else
Response.Write "<h1>Welcome, Guest!</h1>"
End If
' Server generates timestamp
Response.Write "<p>Page generated at: " & Now() & "</p>"
%>
</BODY>
</HTML>
Comparing Execution Models
Client-Side Scripting
The web server doesn't process client-side scripts at all. Only the client processes them. It's the client's responsibility to execute all client-side scripts:
Code executes in the user's browser
Users can view and modify the code
Requires browser support for the scripting language
Can access browser features and DOM
Provides immediate user feedback without server communication
Client-Side Form Validation
<SCRIPT LANGUAGE="JavaScript">
function validateForm() {
var email = document.forms["contactForm"]["email"].value;
if (email == "") {
alert("Email must be filled out");
return false;
}
// Check email format
var emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailPattern.test(email)) {
alert("Please enter a valid email address");
return false;
}
return true;
}
</SCRIPT>
<FORM name="contactForm" onSubmit="return validateForm()">
Email: <INPUT type="text" name="email">
<INPUT type="submit" value="Submit">
</FORM>
Server-Side Scripting
Server-side scripts get processed completely on the web server. The client doesn't receive any code from server-side scripts—just the output. Client-side scripts and server-side scripts can't interact directly because they execute at different times and places:
Code executes on the web server
Source code remains hidden from users
Works regardless of browser capabilities
Can access databases and server resources
Processes before sending anything to the browser
Server-Side Database Query
<%
' This runs on the server only
Dim conn, rs, sql
' Connect to database
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=MyDatabase"
' Execute query
sql = "SELECT ProductName, Price FROM Products WHERE Category='Electronics'"
Set rs = conn.Execute(sql)
' Generate HTML from results
Response.Write "<table border='1'>"
Response.Write "<tr><th>Product</th><th>Price</th></tr>"
While Not rs.EOF
Response.Write "<tr>"
Response.Write "<td>" & rs("ProductName") & "</td>"
Response.Write "<td>$" & rs("Price") & "</td>"
Response.Write "</tr>"
rs.MoveNext
Wend
Response.Write "</table>"
rs.Close
conn.Close
%>
Combining Both Approaches
You can use both server-side and client-side scripting together effectively. The server-side code runs first and can generate client-side code dynamically:
Combining Server and Client Scripts
<%@ Language="VBScript" %>
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function showWelcome() {
var userName = "<%= Session("UserName") %>";
alert("Welcome back, " + userName + "!");
}
</SCRIPT>
</HEAD>
<BODY onLoad="showWelcome()">
<%
' Server-side: Check if user is logged in
If Session("UserName") <> "" Then
Response.Write "<h1>Dashboard</h1>"
Response.Write "<p>Last login: " & Session("LastLogin") & "</p>"
Else
Response.Redirect "login.asp"
End If
%>
</BODY>
</HTML>
Key Takeaways
Understanding that client-side and server-side scripts are two completely separate entities is essential. They execute at different times, in different locations, and serve different purposes. Client-side scripts handle user interactions and provide immediate feedback. Server-side scripts handle business logic, database operations, and generate dynamic content. Use each where it makes the most sense for your application.
Quick FAQ
Can client-side and server-side scripts interact with each other?
No, client-side and server-side scripts cannot directly interact because they execute at different times and locations. Server-side scripts run first on the server and finish completely before any client-side scripts execute in the browser. However, server-side code can generate client-side code dynamically, allowing you to pass data from the server to client-side scripts.
When should I use server-side scripting versus client-side scripting?
Use server-side scripting for database operations, sensitive business logic, user authentication, and generating dynamic content. Use client-side scripting for form validation, user interface interactions, animations, and immediate feedback without server round-trips. Often you'll use both together for the best user experience.
Why doesn't the browser receive ASP code?
The web server processes all ASP code completely before sending anything to the browser. Only the output generated by your ASP code reaches the client, never the code itself. This protects your source code and ensures browser compatibility since the browser only receives standard HTML. It's like the server showing you the finished meal instead of the recipe.