@
First of all, we have to organize our page to easily find any part of the code we insered. It`s very important for you to put signs or usefull information in case you don`t remember what it was.
P.S.: This is a
tutorial, so please read carefully all the information before using any code.
1) First step of the page
<%@Language = "VBScript"%>
<%
Option Explicit
response.expires = 0
response.expiresabsolute = Now() -1
response.addHeader "pragma","no-cache"
response.addHeader "cache-control","private"
Response.CacheControl = "no-cache"
%>
I always use a code in ASP, which has a particular function:
- To force the browser to read the web page from the server, and not from the cache of the
computer.
For persons who are not pretty familiar with this, don`t worry, because s quite simple: everytime a page is loaded by a browser, depending on the configurations of couse, it will save some information about this site, specially if you see it with
frequently. If that happens, the next time you try to see the page, it will be
displayed the stored file from that address, not the real page from the server.
As ASP pages are known because they are dinamical, we would lose this
functionallity. This function just asks the browser to not keep any
"cache" inside the computer. Later, you will see that this same code
will help you to keep the "Logout" (particular page to make the log
out inside a web page) more secured.
- To request that all
variables to be Dimensioned.
It`s pretty usefull because it helps us to check if there is any variable we are
using by mistake, or incorrectly. t`s not a necessary command, but I advise, to
keep the programming sequence more organized.
Bu,
it also has its problems:
- I said that it will refresh everytime from
the server, not keeping any info inside the cache. The problem is that
everytime, the page will load all images again, even if they are the same. For
non fast internet connections, it might be a difference. But, anyway, if you
want something very dinamic, I suggest to use it, or the visitor won`t be able
to see your dinamic page...
@
2) Inserting the explanation
In ASP code, the symbol used to ignore a code
line is " ' ". But you can use it in any place, being aware of the
details:
- After you used this char, on the same line,
any code won`t be seen as a code, but as text.
Eg:
<%
'This line is text
Dim var1 'This var is for counting
Dim var2 'This var is for catching
%>
@
3) Organizing the page functions - DIM
I prefer to guide all variables to the top of
the page, even before the HTML tags. It helps you not using the same variable
twice. In long programs, it may happen.
Another thing is to dimension a variable with a
characteristic name that will help you to catch its meaning right away. Avoid
using simple letters like "i" or "var". It will become a
mess for long programming. Don`t be afraid of calling a variable as "TextFromFormToBeChanged".
The computer will accept it and you will never forget the idea of its value. As
you see, we usually try to put the first letter of a word in CAPS, to be easier
to read it later.
@
4) Functions, Sequences & Misc.
Just a part to say how much interest it is when
you organize your code in a way you can see the structure and recognise quickly
what is going on. For example, let`s see the command For - Next.
<%
For i=1 to 10
response.write i
Next
%>
When you see this code, it`s not quite
difficult to get its idea. It will print on screen the sequence of numbers from
1 to 10. And, if this code is inside some another code, you won`t mistake its
function. But, this is a case we usually don`t use, you should know this by now.
That`s why we try to separate dependent lines with the "TAB" button...
This would turn to:
<%
For i=1 to 10
response.write i
Next
%>
It does not seem to be changed a lot, but, with
long programming, believe, it`s very important.
I took one example I usually do in my websites,
to check this idea...
This next code check if a variable has a value,
then randomizes for a value that must be different from the first one. But don`t
worry with its funcionallity, that`s not that intention right now.
<%
If request("CorDefinida") = "" then
Randomize
CorRandom = Int((TotalCores * Rnd()) + 1)
if CorRandom = Session("CorSite") then
While CorRandom = session("CorSite")
randomize
CorRandom = Int((TotalCores * Rnd()) + 1)
Wend
end if
Session("CorSite") = Cores(CorRandom)
else
Session("CorSite") = request("CorDefinida")
end if
%>
But, look to the difference of reading this
code, and the next one...
<%
If request("CorDefinida") = "" then
Randomize
CorRandom = Int((TotalCores * Rnd()) + 1)
if CorRandom = Session("CorSite") then
While CorRandom = session("CorSite")
Randomize
CorRandom = Int((TotalCores * Rnd()) + 1)
Wend
end if
Session("CorSite") = Cores(CorRandom)
else
Session("CorSite") = request("CorDefinida")
end if
%>
What do you think? Makeing the code clear like
this, you help yourself getting bugs, and even improving the code later.
Happy programming... |