Redirect user with ASP code in <body> ?

N

Noozer

Is it possible to redirect a user to another web page using ASP code located
in page body?

I have some ASP code that runs after generating some output to the browser.
It checks for a condition that can only be calculated using the values
produced in the output, so redirecting the user before the <html> tag isn't
possible without running the code twice (which I'd like to avoid.) - so
RESPONSE.REDIRECT "url" won't work.

Any suggestions?
 
R

Roji. P. Thomas

Set Response.Buffer = true

Then you will be able to redirect before flushing the buffer output to the
client.
 
A

Aaron Bertrand [SQL Server MVP]

It checks for a condition that can only be calculated using the values
produced in the output

I don't understand why there has to be any HTML output before these values
are produced.
, so redirecting the user before the <html> tag isn't
possible without running the code twice (which I'd like to avoid.)

Why do you have to run any code twice?

Instead of saying:

<%=x+y%>

Just say:

<%
mySum = x + y
%>

Then later you can say,

<%
if mySum > 10 then
response.redirect "place"
else
response.write "stuff"
end if
%>
- so RESPONSE.REDIRECT "url" won't work.

Any suggestions?

Yes, if you can't use more sensible logic on the server side, use
client-side script.

<% if mySum > 10 then %>
<script>location.replace('place');</script>
<% end if %>

Now you don't have to alter the buffering behavior of the page, or fight
with the server's settings, etc. I still don't understand why ASP script
would ever have to be run twice...

http://www.aspfaq.com/2262
http://www.aspfaq.com/2011
http://www.aspfaq.com/2217
 
N

Noozer

Yes, if you can't use more sensible logic on the server side, use
client-side script.

<% if mySum > 10 then %>
<script>location.replace('place');</script>
<% end if %>

Now you don't have to alter the buffering behavior of the page, or fight
with the server's settings, etc. I still don't understand why ASP script
would ever have to be run twice...

Your entries:<br />
<%
for i = 1 to 20
if Request.Form("Entry" & i)="" then
Response.Redirect "EntryPage.asp"
end if
Response.Write "You entered " & request.form("Entry"&i) & " for #" & i &
".<br />" & vbcrlf
next i
%>
Done!<br />
 
A

Aaron Bertrand [SQL Server MVP]

Your entries: said:
<%
for i = 1 to 20
if Request.Form("Entry" & i)="" then
Response.Redirect "EntryPage.asp"
end if
Response.Write "You entered " & request.form("Entry"&i) & " for #" & i &
".<br />" & vbcrlf
next i
%>
Done!<br />

How about:

<%
for i = 1 to 20
line = "You entered _e_ for #_i_.<br />" & vbcrlf

e = Request.Form("Entry" & i)
if e = "" then
response.redirect "EntryPage.asp"
response.end
else
output = output & replace(replace(line, "_e_", e),"_i_",i)
end if
next

response.write output
%>

My main question, I guess re-worded, is, "why bother writing out 19 lines
the user won't have time to see, if they didn't fill out the 20th entry?"
And that leads to another question, why let them submit the form at all, if
they are going to have to go back and start over? It seems you need to
learn a little bit about usability... I would be pissed if you let me fill
out 19 items, then sent me back to an empty form because you didn't bother
*forcing* me to fill out the 20th item.

A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top