Managing Buffer to post data outputs to client side
|Through buffering we can control the flow of data to user browser. We can send the date by chunk (or group ) before the complete processing of the page is done or we can send the outputs after the complete script execution is over. Many times we want to send some data for the visitor to read and meanwhile continue the processing of the script. Some time we don't want any data to be send to browser before the complete page processing is over. We can manage all these by handling the buffer.
Buffer is to be turned OFF if we want the data to go the client browser as and when processed. So at different time different outputs reaches the browser. If the Buffering is ON then all outputs are collected in the buffer and posted once the script execution is over. In IIS version 5.0 the buffer is ON by default.
We have to set the status of buffer before any output is posted, otherwise we will get error message. So best is to set the buffer status at the staring of the page. Any output if coming due to any warning or error message in script part will create problem if we keep the buffer just before the html tags.
The best way to test the buffer effect is to check the date and time up to seconds level by using now function and printing to screen.
<%@ Language=VBScript %>
<% Option Explicit %>
<% Response.Buffer = TRUE %>
<title>(Type a title for your page here)</title>
For i=1 to 10000000
j= j+1 'to waste some time
Response.Write "<br>" & now
Here in the above code two places the data or the output is to be posted to the user browser. Both the data are current date and time. So we can see the difference in seconds. If in your case the difference is too short or too high then adjust the looping number by changing number if zeros in for loop. Here you can see the for loop is there only to add some delay between two response.write statements printing the date and time to the user screen.
Here as buffer is ON †(Response.Buffer = TRUE ) so you can see two different times getting displayed on the screen at a single instance of time. Here both times or time outputs are stored in buffer and posted at one go once the script execution is over.
Try the same code with buffer is OFF or false . Change this line only and keep it as False.
<% Response.Buffer = False %>
You can see the difference as first one output of time will come and after some time the other out put of time will come ( change the value of looping in for loop if gap is more or too less, this is to adjust delay ). Here each line is posted immediately without waiting for the full page to complete its execution.
If we keep the buffer Off and then send output and then try to make the buffer On then we will get an error message like this
Response object error 'ASP 0156 : 80004005'
The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.
Similarly we canít make buffer False when it is On before and send some output in between. Here is the error message we will get
Response object, ASP 0157 (0x80004005)
Buffering cannot be turned off once it is already turned on.