ASP 语法

VBScript 基本语法、输出方式、注释、大小写与语句分隔。

Beginner
Key PointsVBScript 不区分大小写、语句不用分号、字符串用双引号、注释用单引号。

三种输出

ASP
<%
    ' 方式一:Response.Write
    Response.Write "Hello<br>"

    ' 方式二:<%= %> 短标签,等价于 Response.Write
%>
<p><%= "Hello" %></p>

<%
    ' 方式三:直接写在 HTML 里(静态内容)
%>
<p>Hello</p>
Output
Hello
Hello
Hello

<%= expr %> is shorthand for Response.Write expr. Use the short tag for HTML-heavy output and Response.Write for string building.

注释

ASP
<%
    ' 这是单行注释
    ' VBScript 没有块注释,多行就写多行单引号

    Rem 也可以,但只在一行开头有效
    Dim x   ' 行尾注释同样可以
%>

<%-- 这不是 VBScript 注释,ASP 里不支持 --%>
<%-- --%> is JSP syntax; ASP does not recognise it. The contents are treated as server code and raise a syntax error. ASP has only ' and Rem.

大小写与语句

ASP
<%
    Dim UserName
    username = "Ada"          ' 同一个变量
    Response.Write USERNAME   ' 输出 Ada

    ' 一行多条语句用冒号
    Dim a, b : a = 1 : b = 2
    Response.Write a + b
%>
Output
Ada
3

VBScript is case-insensitive: UserName, username and USERNAME are one variable. That reads easily but invites typos that silently create new variables — always start with Option Explicit.

Option Explicit

ASP
<% Option Explicit %>
<%
    Dim total
    totl = 10          ' 会报错:变量未声明
    total = 10         ' 正确
%>
Output
Microsoft VBScript runtime error '800a01f4'
Variable is undefined: 'totl'

Option Explicit forces declaration, so a typo fails immediately instead of silently creating a variable. Without it totl = 10 creates a new variable and total stays empty — a very hard bug.

字符串与引号

ASP
<%
    Dim s
    s = "He said ""hi"""          ' 双引号用两个双引号转义
    Response.Write s & "<br>"

    Response.Write "共 " & Len(s) & " 个字符<br>"
    Response.Write UCase("abc") & "<br>"
    Response.Write Replace("a-b-c", "-", "/") & "<br>"
%>
Output
He said "hi"
11
ABC
a/b/c

VBScript joins strings with &. + also works but does arithmetic when a number appears, so "1" + 2 becomes 3. Always use &.

Friends & Partners