Scripts in VB scripts, you don't have to define variables or explicitly define their types in other scripting languages. A variable exists the first time you use it. However, this feature makes your code widely available in typing. If you incorrectly define a variable name in your code, a new variable will be created. Your script may not work properly, and you may not be aware of the error.
Transfer from: China Software network www.csdn.com.cn
When you use variables, you have to develop a habit of defining them, and all you have to do is test the Dim VariableName:
%<%dim intuserid%>%
Intuserid can now be used. For another safety net, use option Explicit. If you open option Explicit, you will send the wrong signal at any time using the variable. This sounds boring, but when your script goes wrong, he can give you some clues, or you'll have to find out where the error is.
To use option Explicit, take 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 the following 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
Using Len
You can use the Len (string) function to determine the length of a string of text:
<%
Intstring = "This was a simple sentence."
Intstringlength = Len (intstring)
Response.Write "There are" & Intstringlength & "characters (including spaces) in the sentence shown:"
Response.Write "" & Intstring & ""
%>
If you want to know how Len works manually, you may want to ask the user to enter their five-digit code or three-bit pin form. Using Len, you've entered enough numbers.
Use Trim
The trimming string is something you want to get at the beginning. Many times, a string has an extra space at the beginning or end, and if you don't balance it, you may be worried 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 does not match.
End If
%>
If the value of strname is "Amy Cowen" because that is how I input it into the form box and then test whether two variables are the same, the result is not, because "Amy Cowen" is not "Amy Cowen."
Similarly, if you enter name into the URL:
<% Response.Write "& Objrec (" Name ") &" >your Site "%>
If any part of the record in name has extra space, you will quickly execute the error problem.
You can fix a whole string of the latter on the left or right to execute the process:
<% strcomments = Request.Form ("Comments")
strcomments = Trim (strcomments)
%>
Assume that the user has entered::
"I am having problems installing the software I downloaded."
The above trimming statement will break the extra space, leaving only the following:
"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)
Trim on the right, using RTrim (String). Trim on the left, using LTrim (String).
Transformation
When you start using a language similar to VB, you can make some simple mistakes, such as comparing the integer 512 and string 512. If you realize that the previous 512 and the latter 512 are not the same, you can think about why the script given is not working properly.
Imagine that you send a file ID to an ASP script, using Request.QueryString, and you will determine that the ID of the file is what the user wants to edit. You need to enter some information from the database for the records and display them on the screen. The IDs in the database are about the same as the integer type, especially if you use AutoNumber performance in that area. The ID you entered is in fact a string. Therefore, these two can never be matched unless you convert to the same type.
Request.QueryString:
Using FileSystemObject, you can test a text? For example, *.html, *.asp, *.inc, *.gif, or the existence of a directory. If the file exists, you can want a series of time to happen. If the file does not exist, you may need other events to occur, using 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 this 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"
' The name of ' a ' of the ' a ' have to this variable.
' strFileName holds just the name, not the extension or the path.
' Make sure your change the path ' of spath to the virtual directory your file is
' Run the script.
' Then come the strFileName variable to the name of a file
' Do not have.
' Run the script.
Changing the input code into an integral type is very easy and can make your future work easier, and the sample format is this:
<%
Dim Intuserid Intuserid = Request.QueryString ("UserID")
Intuserid = CInt (Intuserid)
' Intuserid is now ' integer.
%>
You can also annotate your ASP code and use them.
Comment Code
In ASP, a script is executed before it is sent to the browser, so you do not need to use a normal HTML annotation tag to hide the script from the old browser. In fact, your script will not be displayed in the HTML source, because the source code is handed over by the browser, so the old browser will not suddenly launch any code to the screen.
You may want to annotate your ASP script, and in VB Script you can use ellipses to record annotations:
<%
currentdate = Now
' Make sure your use quotation marks around T