In VB script, you don't have to define variables or explicitly define their types like in other scripting languages. A variable exists the first time you use it. However, this feature makes your code widely available in typescript. If you define a variable name incorrectly in your code, a new variable will be created. Your script may not work properly, and you may not be aware of the error.
%<%Dim IntUserID%>%
IntUserID is now available. For another safety net, use Option Explicit. If you turn on Option Explicit, you will emit an error signal any time you use a variable. This sounds tedious, but it can give you some clues when an error occurs in your script, otherwise you would have to work hard to figure out where the error is.
To use Option Explicit, put the following as the first line of your script:
<% Option Explicit %>
If you want to see what happens when you forget to define a variable, you can run this code:
<% Option Explicit %>
<:% strName = Request.Form("Name") %>
Because the strName variable (Dim strName) is not defined, you will see the following errors occur:
Microsoft VBScript runtime error '800a01f4'
Variable is undefined: 'strName'
/e/oe-test.asp, line 10
uses Len
You can use the Len(string) function to determine the length of a text string:
<%
IntString = "This is a Simple Sentence."
IntStringLength = Len(IntString)
Response.Write "There are " & IntStringLength & " characters (including spaces) in the sentence shown below:"
Response.Write "" & IntString & ""
%>
If you're wondering how Len works manually, think about the form in which you ask users to enter their five-digit code or three-digit PIN. Using Len, you verify that you have entered enough numbers.
Use Trim
Trimming strings is something you want to get right at the beginning. Many times, a string has an extra space at the beginning or end, and if you don't balance it, you might worry about wasting time on those variables.
<% strName = Request.Form("Name")
strCheckName = "Amy Cowen"
If strName = strCheckName THEN
Response.Write "Success! The names matched."
Else
Response.Write "Sorry. The names do not match."
End if
%>
If the value of strName is "Amy Cowen", because that is how I enter it into the form box, and then test whether the two variables are the same, it is not, because "Amy Cowen" is not "Amy Cowen."
Likewise, If you enter Name into a URL:
<% Response.Write " & objRec("Name") & "">Your Site" %>
If any part of the record in Name has extra space, you will quickly run into errors question.
You can modify a whole string of processes to execute on the left or right:
<% strComments = Request.Form("Comments")
strComments = Trim(strComments)
%>
Assume that the user has entered:
"I am having problems installing the software I downloaded. "
The trimming statement above will break up the extra space, leaving only the following content:
"I am having problems installing the software I downloaded." "
Now, back to our "Amy Cowen" example, if I add the following script, we will succeed:
strName = Trim(strName)
To trim on the right, use Rtrim(string). To trim on the left, use Ltrim(string ).Conversion
When you start using a language similar to VB, you will make some simple mistakes, such as comparing the integer 512 with the string 512. If you realize that the previous 512 and the next 512 are different, you can think about why the given script does not work properly.
Imagine that you pass a file ID to an ASP script, using Request.QueryString, and you will make sure that the file ID is the one the user wants to edit. You need to enter some information for the records from the database and display them on the screen. IDs in the database are similar to integers, especially if you use the AutoNumber feature in that area. The ID you entered is actually a string. Therefore, these two can never match unless you cast to the same type.
Request.QueryString:
Using FileSystemObject, you can test the existence of a text—for example, *.html, *.asp, *.inc., *.gif—or a directory. If the file exists, you can want a sequence of events to occur. If the file does not exist, you may want other events to occur, use the following code: <%
sPath="/profiles/" & strFileName & ".asp"
sFile=Server.MapPath(sPath)
Set fe=Server.CreateObject("Scripting.FileSystemObject")
if fe.FileExists(sFile) THEN
'do something
Response.Write "Yeah! I found it!."
Response.Write "You can access that file by "
Response.Write "<A HREF=""" & sPath & """>Clicking Here</A>."
else
'do something
Response.Write "Sorry. The requested file does not exist."
end if
%>
To simply test your file, add the script to the top:
strFileName = "name"
' First assign the name of a file you have to this variable.
' strFileName holds just the name, not the extension or the path.
' Make sure you change the path for sPath to the virtual directory your file is in
'Run the script.
' Then come back and change the strFileName variable to the name of a file
' you do NOT have.
' Run the script.
Changing the entered code to an integer is very easy and will make your future work easier. The sample format is:
<%
dim intUserID intUserID = Request.QueryString("userID")
intUserID = CInt(intUserID)
' intUserID is now an integer.
%>
You can also comment your ASP code and use them.
Comment code
In ASP, a script is executed before it is sent to the browser, so you don't need to use normal HTML comment tags to hide the script from older browsers. In fact, your script won't be displayed in the HTML source because that source code is handed over by the browser, so older browsers won't suddenly fire any code to the screen.
You may want to comment your ASP scripts with comments. In VB scripts, you can use ellipses to record comments:
<%
currentdate = now
' make sure you use quotation marks around the HTML code
' the & serves to concatenate the string
Response.Write "Today is: " & currentdate & "<BR>"
%>
Use // in ASP scripts to indicate comments
using include If you have used .shtml files before, you may have Familiar with the included working process, if not, we will tell you step by step how to use it.
An include is a capacity, which is stored in a separate file. In standard HTML, includes usually use the .inc extension. In ASP, you use the .asp, .txt or .inc extension. Then you will call the file into the HTML code. When the HTML file is sent, the include file is taken out of the special file and written directly to the screen as the content of the HTML. Therefore, if you make a view source code, the display page you see will be the same as what you input.
For a plain HTML, it will save a lot of time when creating a template for the site. Header, footer, and navigation elements are part of what appears on a large number of your pages. When you use includes, you enter the file once and call it when needed. This has several advantages:
When updating content, you only need to update one file.
The inclusion of meat is isolated from the HTML page, so anyone who wants to make changes to the content can avoid messing with the script.
Your core HTML files are smaller and more functional.
In ASP, inclusion is priceless. You can use the script over and over again, sometimes it can even be as simple as a connection string stored in the include, and it can be deleted when the page is not available.
To use include files, you use either of these formats:
<!-- #INCLUDE FILE="filename.inc" -->
or
<!-- #INCLUDE VIRTUAL="/filename.inc" -->.
File reference Contains the relevant paths used. VIRTUAL references an absolute path to the associated server. If you want to move directories and files, the easiest way is to use VIRTUAL by default so that you don't encounter some unexpected errors later. On the other hand, when you test files, you need to use files because it depends on how you set up your personal server.
In standard HTML, you generally use the .inc suffix for your include files. In ASP, you can use .asp. This prevents your files from being easily opened and read by others.