Recommended: How to Format ASP Pagination and Date to RFC822 Format Calculate the page, hehe, you don’t have to judge the following referenced content: intNumPage = Abs(Int(-(intNumRecord/intPerPage))) Format the date
If the computer room is about to close, or if you are in a hurry to date a MM, please jump directly to the fourth paragraph.
The scripts described below include server-side scripts and client-side scripts. Server-side scripts refer to the part of scripts running on the server. For example, the common Response.Write is obviously run on the server. Server-side scripts can be written in VBScript and JScript languages. In this article, VBScript is used, JScript is the same principle.
Client scripts can also be considered to include two languages: VBScript and JavaScript, which are scripting languages that run on client browsers. For example, when we visit a web page, a message box pops up, which is done using client scripts (alert, msgbox, etc), and it is obviously not something that server-side scripts can do. There is a big difference between client scripts and server scripts (in browsers such as IE and Firefox), that is, client scripts can access the document object model (DOM) and operate objects in the page (such as modifying the page title, modifying the innerHTML attribute of a div, etc.).
First, let’s understand the process of ASP page execution
1.IIS finds the ASP file and submits it to the ASP engine (usually ASP.DLL) for processing.
2. The engine opens this ASP file and finds the content between <% and %>, and of course the content between <script runAt=server> and the corresponding</script>. These contents are called script blocks. Only the content in the script block is parsed by the engine, and the other content is ignored, and is inserted between the script blocks as meaningless characters. It is necessary to clarify that in fact, there are more than this content being parsed. The server-side included files of the <!--#include ***--> class are also included and processed by the engine. If you read more programs, you will also know that some <object> objects marked as Server with runAt attribute will also be processed, so I won't discuss it in depth here.
3. The engine executes scripts in the script block. These server-side scripts are executed as a whole, that is, the following code can be written:
| The following is the quoted content: <% Dim i For i=1 to 5 %> Hello World! <% Next %> |
The engine does not parse these script blocks separately, causing syntax errors to occur in both script blocks. So we have come to the following conclusion: not all non-server script code will be sent to the client, and it is possible that this non-server script code is restricted by the script block. The server will definitely not worry about the execution of client scripts, but different client scripts can be output through server scripts.
4. In the end, the engine generates a text stream, or the execution result of the script, which can be considered as a string, which is the code sent to the web page of the client browser. The client browser displays the page. At this time, the source code (source file) of the page does not contain server-side scripts, but contains the execution result of server-side scripts (this is obvious).
<% … %> and <script runat=server>…</script>
They are all server-side scripts that are processed and executed at the same time. They are executed as a whole.
<% … %> and <script language=…>…</script>
The former is a server-side script, and the latter is a client-side script. The former is executed first, and the latter is executed later.
In fact, it is not entirely true. The scripts of the two may be executed at the same time, but the space is different, and it is still: the former is executed on the server, and the latter is executed in the client browser. The former must be logically executed in advance by the latter. At the same time, we also concluded that during the execution of the same page, the client script cannot be fed back to the server script in any way. That is, the client browses your message book and submits a new message, or the value obtained by any client script cannot be processed in the same server response.
About the component's call
Note that server-side scripts and client-side scripts are both scripts, so you can naturally create xmlhttp components, ADODB.Connection components, etc., but not anywhere.
If xmlhttp is used for crawling web pages (such as collection) on the server, it must be created in the server script. If it is used for ajax for the client and the backend accesses the pages on the server without refresh, it is run on the client and naturally created on the client.
The ADODB.Connection component is used to access the database. Generally, it is created on the server side. After all, the server-side asp program is running the database data. However, if your database is really connected to the client (such as http://bbs.bccn.net/thread-224966-1-2.html), then it is undoubtedly created in the client script.
In short, contradictory things and their respective aspects have their own characteristics. Different things have different contradictions; the same thing has different contradictions in different processes and stages of development; the different contradictions in the same thing and the two different aspects of the same contradiction have their own characteristics (if you can't understand them, you can ignore them...). This principle requires us to adhere to the principle of specific analysis of specific problems, and under the guidance of the principle of universality of contradictions, we should specifically analyze the particularity of contradictions, and find the correct way to resolve them. Oppose the same-sized adoption of one method to resolve the contradictions between different things. This is what you say when you open a key and sing a song when you go to the mountain.
The server-side VBScript script uses the Server.CreateObject(className) method to create objects, and the client-side VBScript script uses the CreateObject(className) method to create objects.
Typical error
| The following is the quoted content: <% Function TSize(b) 'This is my custom function TSize=China end function %> <a href=javascript:<%TSize('variable')%> >Click here to use the function I defined</a> (http://bbs.bccn.net/thread-225244-1-1.html) |
Error analysis:
Confuses the difference between server-side scripts and client-side scripts. When actually executing, we will find that the client does not receive any code like TSize at all, because TSize is a server-side program. After being processed by the engine (note that the engine's processing of functions is purely called by the server-side script and will not be sent back to the client) and it will disappear and it cannot work on the client. This means that client scripts cannot directly call functions of server-side scripts.
In fact, this program has syntax errors. When the engine processes this content, it first finds the content between <% and %>, that is, <%TSize('variable')%>. Obviously, this content does not comply with the syntax rules of VBScript. Well, changing it to <%=TSize(variable)%> There is no syntax error in the server-side script. At this time, the TSize function can return the value to China normally, so the href attribute received by the client is written like this: javascript: China, it cannot be executed.
The impact of server-side scripts on client-side scripts
As mentioned earlier, server-side scripts are logically executed in advance of client-side scripts, so such code is feasible:
| The following is the quoted content: <% Dim i For i=1 to 5 Response.Write <script type=text/javascript> _ & alert('Hello World! & i & ')</script> Next %> Regarding the execution of Response.Redirect and javascript Note that the following code is written incorrectly: <% Response.Redirect index.asp Response.Write <script type=text/javascript> _ & alert('Password error!')</script> %> |
This is a common mistake. Writers often think that writing code in this way can cause the client to pop up a password error prompt and then turn to index.asp. In fact, this cannot happen. Even if two lines of code are exchanged in sequence, it is impossible to achieve this effect.
The reason is related to the way the server handles the two lines of code. These two lines of code cannot work at the same time.
Response.Write sends a piece of text to the client. The content of this piece of text can be a script. The client browser can execute this script after receiving it. Note that it can only be executed after receiving it.
Response.Redirect sends an HTTP header to the client (what is HTTP header? Let's put it this way, for example, the write to the client cookies is HTTP header information, and the HTTP header information is sent back to the client browser before the HTTP subject. This is why sometimes we modify cookies after closing the server's buffer, because the subject has started to transmit and is not allowed to send HTTP header information.), the content of the information tells the client browser to jump to the page to browse. Note that this Redirect information works immediately, that is, this Redirect information is exclusive. When the buffer is turned on, no matter how much content has been written into the buffer using Response.Write, once Response.Redirect is called, the buffer will be cleared and the header instruction will be sent to the client browser. If we dynamically track the execution of the program, we will also find that after calling Response.Redirect, the program stops executing, so please note that the server-side program must close the data connection and other operations before calling Response.Redirect.
So how should the above example be modified? If you are unwilling to modify that index.asp to add script prompts, you can only put the steering command into the client script to execute, like this:
| The following is the quoted content: <% Response.Write <script type=text/javascript> _ & alert('!');location.href='index.asp'</script> %> |
Share: ASP 3.0 Advanced Programming (33) 7.4.2 VBScript Error Handling In VBScript, the script interpreter can not handle any errors it finds, and continue to run the next statement using the On Error Resume Next statement. Once this statement has been processed, the script engine will continue to run the subsequent program.