First of all, let's take a look at the process of the ASP page execution
1. IIS finds an ASP file and submit it to the ASP engine (usually ASP.DLL) processing.
2. The engine opens this ASP file and finds the content between <%and%>. Of course, there is the content between <script runat = server> and the corresponding </script>. These contents are called script blocks. Only the content in the script block is analyzed by the engine, and the other content is inserted between the script blocks as meaningless characters. It is necessary to explain that in fact, the content of the analysis is more than that. If you read a lot of programs, you will also know that some Runat attributes are marked as Server's <Object> object.
3. The script in the script block of the engine. The scripts of these servers are executed as a whole. That is to say, the following code can be written:
<%
Dim I
For i = 1 to 5
%> Hello World!
< % Next %>
The engine does not analyze these script blocks separately, but makes syntax errors in both script blocks. So we get the following conclusions: Not all non -server scripts will be sent to the client. It is possible that this non -server script code is limited by the script block. The server must not worry about the execution of the client script, but you can output different client scripts through the server's script.
4. The final engine generates a text flow, or the execution result of the script. It can be considered that this is a string, which is the code sent to the web page of the client browser. The client browser is displayed on the page. At this time, the source code (source file) of the page does not include the server's script, but it contains the execution result of the server script (this is obvious).
< %… %> And <script runat = server> ... </script>
They are the server -side scripts and are processed and executed at the same time. When they execute, they were as a whole.
< %… %> And <script language = ...> ... </script>
The former is the server script, the latter is the client script. The former is executed first, and the latter is executed.
In fact, the script of the two may be executed at the same time, but the space is different. It is still: the former executes on the server and the latter is executed in the client browser. The former must be logically executed in advance. At the same time, we also have a conclusion: In the execution of the same page, the client script cannot be fed back to the server script anyway, that is, the client browse your message book and submit a new message or any client script acquisition value It is impossible to be processed in the same server response.
Call for components
Note that the server scripts and client scripts are script. Naturally, you can create XMLHTTP components, adodb.connection components, etc., but it is not placed everywhere.
If XMLHTTP is used for the server's grabbing webpage (such as collecting), it will be created in the server script. If it is used for the client's AJAX brushless refresh and visiting the server on the back of the server, it is running on the client. Created.
Adodb.Connection component is used to access the database. Generally speaking, it is created on the server side. After all, the ASP program on the server is running the database data, but if your database is really connected on the client, then there is no doubt that it is on the customer on the customer Created in the end script.
In short, contradictory things and each side have their own characteristics. Different things have different contradictions; different processes and different stages of the same thing have different contradictions in the development of the same thing; different contradictions in the same thing and the two different aspects of the same contradiction have their own particularity (if you do not understand Don't look ...). This principle requires us to adhere to the specific analysis principles of specific issues. Under the guidance of the principle of universality of contradictions, specific analysis of the particularity of contradictions, and finding the correct method of resolving contradictions. Opposition to use one method to solve the contradictions of different things. A key is opened, what kind of mountain sings and what kind of song is the truth.
The server -side VBSCRIPT script creation object uses Server.CreateObject (ClassName) method. The client VBScript script creation object uses CreateObject (ClassName) method.
Typical error
<%
Function Tsize (b)
'This is my custom function
Tsize = China
end function
%>
<A href = javascript: <%tsize ('variable')%>> Click here to use my definition function </a>
Error analysis:
It confuses the difference between the server script and the client script. During the actual execution, we will find that the client does not receive any code such as TSIZE, because Tsize is a program on the server, and after the engine is processed (note that the engine's processing of the function is purely called for the server script, it will not be It disappeared, and it was impossible to play a role on the client. That is to say, the client script cannot directly call the server -side script function.
In fact, this program is wrong with grammar. When the engine processs this content, first finds the content between <%and%>, that is, <%TSIZE ('variable')%> VBScript's grammatical rules. Well, change to <%= TSIZE (variable)%> there is no grammar error at the server -side script. At this time, the TSIZE function can return to China normally, so the HREF attribute received by the client is written like this: JavaScript: China, China, It cannot be executed.
The effect of server script on the client script
As mentioned earlier, the server script is logically executed in the client script in advance, so such a code is feasible:
<%
Dim I
For i = 1 to 5
Response.write <script type = text/javascript> _
& Alert ('Hello World! & I &') </script>
Next
%>
About the execution of response.redirect and JavaScript
Note that the writing of the following code is wrong:
<%
Response.redirect index.asp
Response.write <script type = text/javascript> _
& alert ('password error!') </script>
%>
This is a common error. Writers often think that writing code so that the client can first pop up the password error and turn to Index.asp. In fact, this cannot happen. It may achieve this effect.
The reason is related to the handling of the two lines of code. The two lines of code cannot work at the same time.
Response.Write is a text to the client. The content of this text can be a script. Then the client browser can execute this script after receiving it. Note that it can be executed after receiving it.
The Response.ReDirect sends a HTTP header information to the client (what is the HTTP header information? So, for example, the writing of the client cookies is HTTP header information. Browser, this is why sometimes we modify the cable of the server and modify the wrong cookies, because the subject has begun to be transmitted, and it is not allowed to send HTTP header information. Note that this Redirect information works immediately, that is to say, this Redirect information is exclusive. In the case of buffering, no matter how many contents have been written in the buffer, once the response.redirect is called, it will be called and will be called. Will clear the buffer and send this head instruction to the client browser. If you dynamically track the execution of the program, we will also find that after calling response.redirect, the program stops executing, so pay attention to the server -side program before calling response.redirect.
So how should the above example be modified? If you don't want to modify the index.asp to add the script prompt, then you can only put the steering instruction in the client script for execution, just like this:
<%
Response.write <script type = text/javascript> _
& alert ('!'); local.href = 'index.asp' </script>
%>