Important alert: (current site time 7/15/2013 10:57:51 PM EDT)
 

VB icon

A better response.redirect

Email
Submitted on: 2/14/2000
By: Ian Ippolito (vWorker)  
Level: Intermediate
User Rating: By 10 Users
Compatibility: ASP (Active Server Pages)
Views: 95594
author picture
(About the author)
 
     The response.redirect method is great for moving a user from one page to another. However, it does have one major limitation--you can only use it if absolutely nothing has been written to the page. Sometimes, you actually do want to write something to the page and then redirect it, for example when displaying the status of an operation. In this case, the following JavaRedirect function comes in handy. It requires a Java compatible browser. (Note:this script has been changed to incorporate Lewis Moten's helpful suggestions).
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: A better response.redirect
' Description:The response.redirect method is great for moving a user from one page to another. However, it does have one major limitation--you can only use it if absolutely nothing has been written to the page. Sometimes, you actually do want to write something to the page and then redirect it, for example when displaying the status of an operation. In this case, the following JavaRedirect function comes in handy. It requires a Java compatible browser. (Note:this script has been changed to incorporate Lewis Moten's helpful suggestions).
' By: Ian Ippolito (vWorker)
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=0&lngWId=4'for details.'**************************************

sub JavaRedirect (strURL)
'Response.Write "redirect to:" & strURL
%>
<SCRIPT language="JavaScript">
<!--
window.location.href = 
'<%=URL%>';
//-->
</SCRIPT>
<%
end sub


Other 34 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
4/21/2000 8:19:55 AMcpsarros

Excellent simplicity
(If this comment was disrespectful, please report it.)

 
6/3/2000 11:55:28 AMTony

Err... It would be much easier to set the following line server-side:
(If this comment was disrespectful, please report it.)

 
6/3/2000 12:10:37 PMTony

Response.Buffer = True
(If this comment was disrespectful, please report it.)

 
6/5/2000 12:12:34 PMIan Ippolito

Tony,

I appreciate the feedback but doing response.buffer = true doesn't quite do the same thing. If you use that method, the redirect will succeed, but you won't actually see the text that was output to the screen before hand. That won't work for the 'status' situation described.

Ian

(If this comment was disrespectful, please report it.)

 
6/17/2000 7:52:32 AMserkan

this is so cool. thnx.
(If this comment was disrespectful, please report it.)

 
7/21/2000 12:32:00 PMLewis Moten

If you want to let users see content before redirecting them, you can always use META tags to refresh the page to another URL after a specified amount of time. More browsers would be compatable with this method. Your code doesn't even use timers to give the users time to see what you have written. Also, your javascript is in a vbScript format. 1. javascript is case sensitive 2. javascript uses {} to define content within if comparisons. this will definately crash your browsers. here is the real deal. '; //--> There is no need to test for netscape browsers by doing it this way.
(If this comment was disrespectful, please report it.)

 
7/21/2000 4:45:28 PMIan Ippolito

Lewis,

Thanks for the feedback, but the META tag method only works if you know in advance how long the operation is going to take, and alot of times you don't. For example on Planet Source Code, I use the above script when an author submits code so I can display a status. I can't predict in advance how long the upload will take, because each upoad is a different size and each user has a different speed connection.

Regarding {} to wrap comparisons--I've never read any Javascript book describing this as a legal way to do comparisons.

However, I do like your final one line solution. I don't know why I originally did the trap for Netscape that way (this is kind of old code...)

(If this comment was disrespectful, please report it.)

 
7/22/2000 4:36:34 PMLewis Moten

Cool. Another approach would be to have a function called after the page completly loads . then the function would call a timer to change the location after the user has some time to read the page. There are many different approaches to this situation. I was just unaware of the specific requirements. I usually use the {} tags just about everywhere so that i can stay away from any possible errors that may arise in different versions of javaScript. thanks for the reply.
(If this comment was disrespectful, please report it.)

 
10/3/2000 1:13:25 PMMerlyn

You can use the Response.Redirect even if you have written to the page by using Response.Buffer = True in this case all data to the page is buffered until the page is processed or you force the contents of the buffer to the page.
(If this comment was disrespectful, please report it.)

 
10/7/2000 12:58:20 PMIan Ippolito

Merlyn,

Thanks for the feedback...but this is the same thing Tony said above. See the response to his message...

Ian
(If this comment was disrespectful, please report it.)

 
10/19/2001 4:30:07 PMMike Wees

Ian,

I have a question related to this, I couldn't find it answered anywhere else. I'm trying to program a GoBack routine. I want to emulate the user pressing the Back button on the browser so that all the form values are retained. Any idea on the code for this? Thanks in advance.

MW
(If this comment was disrespectful, please report it.)

 
11/16/2001 11:18:38 AMJohn Tolar

Actully, the last question had nothing to do with the topic or code. But, try history.Back();
(If this comment was disrespectful, please report it.)

 
2/18/2002 2:37:29 PMImvain

For comparison to the Javascript solution.

Im kind partial to the location.replace(); function.
(If this comment was disrespectful, please report it.)

 
2/19/2002 11:29:46 AMA1 Programmer

If the user Presses the [ Back ] Button on the browser, the form values will be Retained, but anyway...

href=
(If this comment was disrespectful, please
report it.)
 
2/19/2002 11:30:55 AMa1 programmer

Back or Back
(If this comment was disrespectful, please report it.)

 
3/4/2002 11:20:03 PMSam Moses

good stuff. This approach never occurred to me. Thanks for the insight.
(If this comment was disrespectful, please report it.)

 
5/16/2002 11:42:44 AMxmetrix@aol.com

response.buffer=true
response.flush
response.redirect(
(If this comment was disrespectful, please report it.)

 
5/16/2002 12:36:13 PMIan Ippolito (psc)

Thanks xmetrix, but like the others, it still doesn't help with the situation described above "Sometimes, you actually do want to write something to the page and then redirect it". The code you put up would never display anything.

Ian
(If this comment was disrespectful, please report it.)

 
9/13/2002 4:13:29 AM

Hi, how can I use Response.Redirect and target the page if I am using either frames ot Iframes. Many thanks, Mike
(If this comment was disrespectful, please report it.)

 
9/13/2002 4:16:11 AM

Hi, how do I use Response.Redirect with frames if I want to break out of the frames and use target="_parent". Many thanks, Mike
(If this comment was disrespectful, please report it.)

 
9/13/2002 11:19:22 PMIan Ippolito (psc)

Mike,

Javascript can bust you out of the frameset, but I don't think response.write will do the trick, because it will only basically redirect the user in the current frameset/pane that it is opened up in. Of course there are ways around that...for example, if you set the target for the frame = _parent...then the response.redirect would work. But your situation may or may not allow you to do this.

Ian
(If this comment was disrespectful, please report it.)

 
9/19/2002 2:47:28 PM

i really don´t know what people thinking about. Look my work: keep loling <% some asp code go here lol %>
(If this comment was disrespectful, please report it.)

 
9/19/2002 5:54:37 PMIan Ippolito (psc)

You didnt leave your name, but whomever you are...please read the previous comments before posting. You're the 3rd or 4th person on this thread I've had to explain why the same 'solution' doesn't work for this scenario. I'm not going to do it again...please see above, so we don't have to waste anymore bandwidth on this..thanks.

Ian

Ian
(If this comment was disrespectful, please report it.)

 
9/27/2002 6:08:16 AM

To those trying to 'correct' Ians coding:

I think its cool and all that your trying to put your minds together and make things better but, one of the main reasons i like this code is its simplicity, no meta calls needed and it works!! I think the code is great just as it is.

Thanks
(If this comment was disrespectful, please report it.)

 
12/2/2002 1:51:55 PM

This is great! It help me solve my problem. Now I can disable a submit button after click on it and before redirect it to a new page. This can avoid duplicate form submissions.
(If this comment was disrespectful, please report it.)

 
12/4/2002 10:22:18 AM

I just wanted to say that this code rocks! I didnt use all of the original code, but it was good enough for me to take it and make it do what I needed. Thanks!!

Eric Krauss
Application Programmer
(If this comment was disrespectful, please report it.)

 
4/28/2003 1:23:45 PM

I am new to ASP. Actually I am very new to ASP. There is no comment for the codes. Is the code suppose to display the status of redirecting to a new page?

Can you explain this part?
*****************************
';
//-->
***********************************
it looks like you commented the whole thing out.
(If this comment was disrespectful, please report it.)

 
4/28/2003 1:50:23 PMIan Ippolito (RAC)

No it doesn't display a status..just redirects the user to the page.

The comment is a 'trick' to make sure that it is only seen by browsers that support the Javascript tag (and is seen as a comment by those that don't). This code was written a long time ago, when not all browsers did support it. Today, most do, and it's pretty safe to remove the start and end comments.

Ian
(If this comment was disrespectful, please report it.)

 
5/13/2003 8:43:32 AM

Another handy way is to use server.transfer. Description below

Transfer
The transfer method sends all of the information that has been assembled for processing by one .asp file to a second .asp file.

Syntax

Server.Transfer (path)

Parameters

Path
The location of the .asp file to which control should be transferred.
Remarks

When you call Server.Transfer, the state information for all the built-in objects will be included in the transfer. This means that any variables or objects that have been assigned a value in session or application scope will be maintained. In addition, all of the current contents for the request collections will be available to the .asp file receiving the transfer.
(If this comment was disrespectful, please report it.)

 
6/22/2003 2:27:54 AM

Ian, this bit of code was exactly what I needed for my current project. Works great, and easy to understand. Thanks!
(If this comment was disrespectful, please report it.)

 
6/22/2003 10:11:11 AMIan Ippolito (RAC)

Great...I'm glad to hear it. :)
(If this comment was disrespectful, please report it.)

 
7/1/2003 4:51:47 AM

where to place your code?

i have this lines of code

(If this comment was disrespectful, please report it.)

 
7/1/2003 4:53:34 AM

where to place your code?

i have
this lines of code


<%
Dim rsRecords, sql
Set rsRecords = Server.CreateObject("ADODB.Recordset")

ID = Request("ID")
Response.Write(Request("ID"))

sql = "DELETE ID FROM records WHERE ID= " & ID &" "
Response.Write(sql)
connDatabase.Execute sql
Response.Redirect("listing.asp")
%>
(If this comment was disrespectful, please report it.)

 
7/1/2003 10:17:49 AMIan Ippolito (RAC)

You can place it anywhere...usually at the end of your processing. So place a call to it as the last line of your code.
(If this comment was disrespectful, please report it.)

 
6/20/2004 4:12:07 PM

Like someone else mentioned, the Server.Transfer method is pretty nice as well.

Btw the meta tag has a big drawback in my opinion, that one can easily disable it on IE. Javascript also can be disabled, but is not as easy. Anyway I have used all 3 methods and I stuck with Server.Transfer, as the usage is the same as Response.Redirect:

Server.Transfer "yourpage.asp"

PS. Server.Transfer only works on ASP 3.0
(If this comment was disrespectful, please report it.)

 
7/29/2004 2:24:13 PMPHP Duo

And also, when someone uses frames like hotmail forcibly embeds each of the links in the email body, it's usefull. I always see RAC's link get redirected to the main page destroing frames unlike META refresh where the page is redirected within the sub-frame.
Ian, do you use some other technique for that on RAC?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.