In VB scripts, you do not need to define variables or define their types explicitly in other scripting languages. A variable exists when you use it for the first time. However, this feature allows your code to be widely used in typing. If you incorrectly define a variable name in the code, a new variable will be created. Your script may not work properly, and you may not be aware of this error.
Transfer from: dynamic network Production Guide www.knowsky.com
When using variables, You need to develop the habit of defining them. All you need to do is test dim variablename:
% <% Dim intuserid %> %
Intuserid can be used now. For another security net, use option explicit. If you enable option explicit, you will send an error signal at any time when using the variable. This sounds boring, but when your script is wrong, he can give you some clues, or you will have to find out where the error is.
To use option explicit, use the following content as the first line of your script:
<% Option explicit %>
If you want to see what happens when you forget to define the variable, 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:
Microsoft VBScript runtime error '800a01f1'
Variable is undefined: 'strname'
/E/oe-test.asp, line 10
Use Len
You can use the Len (string) function to determine the length of the 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 want to know how Len works manually, you can think about the form of asking users to enter their five-digit code or three-digit pin. Use Len to verify whether you have entered enough numbers.
Use trim
The trimming string is what you want to get at the beginning. Many times, a string has an extra space at the beginning or end. If you don't balance it, you may worry about wasting time on these 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 strname value is "Amy Cowen", because that is how I input it to the form box, and then test whether the two variables are the same. The result is no, because "Amy Cowen" is not "Amy Cowen."
Similarly, if you enter name in the URL:
<% Response. Write "& objrec (" name ") &" "> your site" %>
If any part of the record in name has additional space, you will be able to quickly execute the error.
You can fix the entire string and execute the process on the left or right side:
<% Strcomments = request. Form ("Comments ")
Strcomments = trim (strcomments)
%>
Assume that the user has entered ::
"I am having problems installing the software I downloaded ."
The above trim statement will free up extra space, leaving only the following content:
"I am having problems installing the software I downloaded ."
Now, go back to our "Amy Cowen" example. If I add the following script, we will succeed:
Strname = trim (strname)
Trim on the right, use rtrim (string). trim on the left, use ltrim (string ).
Conversion
When you start to use a language similar to VB, you will make some simple mistakes, such as comparing integer 512 and string 512. If you realize that the first 512 is different from the last 512, you can think about why the script is not working properly.
Assume that you send the ID of a file to the ASP script and use request. querystring to confirm that the file ID is the one you want to edit. You need to enter some information from the database for the record and display them on the screen. The ID in the database is similar to the integer type. In particular, if you use the autonumber performance in that region. The ID you entered is actually a string. Therefore, the two will never match unless you convert them to the same type.
Request. querystring:
With FileSystemObject, you can test the existence of a text file, such as *. html, *. asp, *. inc., *. GIF, or directory. If the file exists, you can expect a series of times. If the file does not exist, you may need other events. 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"
Response. Write "<a href =" "& Spath &" "> clicking here </a> ."
Else
'Do something
Response. Write "sorry. the requested file does not exist ."
End if
%>
To 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.
It is very easy to change the entered code into an integer and make your future work easier. The sample format is as follows:
<%
Dim intuserid = request. querystring ("userid ")
Intuserid = CINT (intuserid)
'Intuserid is now an integer.
%>
You can also comment out your ASP code and use them.
Comment code
In ASP, a script is executed before it is sent to the browser. Therefore, you do not need to use common HTML annotation labels 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 comment out your asp script. In VB script, you can use an ellipsis to record the comment:
<%
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
Use include
If you have used the .shtml file before, you may already be familiar with the contained work process. If not, we will show you how to use it step by step.
A file contains a capacity, which is stored in a separate file. In standard HTML, include the. inc extension. In asp, you use the. asp,.txt or. inc extension. Then you will call the file to HTML code. When an HTML file is sent, it is included in a special file and directly written to the screen as HTML content. Therefore, if you create a view source code, the display page you see is the same as the one you enter.
For a common HTML, it saves a lot of time when creating a template for the site. Headers, footers, and navigation elements are part of your large number of pages. When using include, You need to input the file once and call it as needed. There are several advantages:
When updating the content, you only need to update one file.
Meat is isolated from HTML pages, so anyone who wants to modify the content can avoid messing up the script.
Your core HTML file is smaller and more practical.
In ASP, inclusion is priceless. You can use scripts over and over again, or even some times, simply store a connection string in the inclusion, and delete it on an unavailable page.
To use include files, you can use any of these formats:
<! -- # Include file = "filename. Inc" -->
Or
<! -- # Include virtual = "/filename. Inc" -->.
File references contain related paths. Virtual references an absolute path to the relevant server. If you want to move directories and files, the easiest way is to use virtual by default, so that you will not encounter unexpected errors later. On the other hand, when you test a file, you need to use the file because it depends on how you set up the personal server.
In standard HTML, you generally use the. inc suffix for your inclusion files. In ASP, you can use. asp. This prevents your files from being easily opened and read by others.