Link to home
Start Free TrialLog in
Avatar of ktjamms2
ktjamms2Flag for United States of America

asked on

redirect to previous page and refresh it

I have a cold fusion shopping cart website. When a user places an item in their shopping cart, it posts to a page that displays all the items placed in their cart. I would like this page to display for a brief time and go back to the previously viewed page but without anything selected(refreshed). I was thinking of using  meta http-equiv="refresh" content="5;url= but I'm not sure of the exact syntac that would go back 1 and  also maintain my #session.urltoken variable.
Avatar of pinaldave
pinaldave
Flag of India image

Hi ktjamms2,
. Declare the body tag in html window as server control by specifing runat attribute and give an id to that tag.

                                <body runat="server" id= "body">< /P>< /FONT>

2. In the code behind, declare this element as htmlgenericcontrol like this.            

  Protected WithEvents body As System.Web.UI.HtmlControls.HtmlGenericControl    

3. Finally add the following code in your page_load event handler, this will add the client side handler for body tag.

 body.Attributes.Add("onLoad", "window.setTimeout(""window.location.href='<somepage>.cfm'"",5000);")    


ANOTHER WAY:::


   <META HTTP-EQUIV="Refresh" CONTENT="3;URL=http://www.some.org/some.html">



ANOTHERWAY :::::::::::::::


<html>
<head>
<title>A web page that points a browser to a different page after 2 seconds</title>
<meta http-equiv="refresh" content="2; URL=http://www.pa.msu.edu/services/computing/">
<meta name="keywords" content="automatic redirection">
</head>
<body>
If your browser doesn't automatically go there within a few seconds,
you may want to go to
<a href="http://www.pa.msu.edu/services/computing/">the destination</a>
manually.
</body>
</html>


Regards,
---Pinal
Hi ktjamms2,
as long as you are worried about your session. it will be there and will not be affected by this code.
If your user does not accept and you want to be extra carefull..in that case this is what you can do... like for your code... you can add #session.URLTOCKEN#  like www.experts-exchange.com&#session.URLTOCKEN#
let me knwo if you have any more questions.

Regards,
---Pinal
Hi ktjamms2,
  Protected WithEvents body As System.Web.UI.HtmlControls.HtmlGenericControl    this may not work... i was testing it and it is not working on my server... so do not waste trying that of my suggestion...

Regards,
---Pinal
Avatar of mrichmon
mrichmon

That looks like ASP code instead of Cold Fusion code ;o)

Personally I would recommend against doing this as the user will NOT appreciate it, but if you do want to proceed the best method for a timed redirect is in PinalDave's answer of this :

<meta http-equiv="refresh" content="2; URL=http://www.pa.msu.edu/services/computing/">

Here is a link that will generate the redirect code for you and even add a progress bar
http://www.designerwiz.com/ad-framer.htm?http&&&www.designerwiz.com/generator/auto_redirect_generator.htm


Here is a good article on types of clientside redirection
http://tech.irt.org/articles/js021/
Hi mrichmon,
 That looks like ASP code instead of Cold Fusion code --- Mrichmon. that is very true. I realize afterwards. My team mate laughed on me and told me that I am so stupid that I can not find out differance between ASP and JAVAScript. He told me that he had this on server, but that is not working. I felt stupid as I posted that first and tested afterwards. thanks again for correcting me.

Regards,
---Pinal
Avatar of ktjamms2

ASKER

the thing is that I don't know the url they just came from to put a specific url location. I want it to automatically go back to the page they were on and refresh so the same selections aren't still on that page so they can re-select different without confusion or having to erase the previously entered form. And I dont want to loose my session.urltoken variable(maybe url.token won't get lost with back and refresh).
Hi ktjamms2,
 And I dont want to loose my session.urltoken variable(maybe url.token
 won't get lost with back and refresh).
It will not get lost if you are worried you can put them in URL as mentioned on the top.
following is javascript code to go back after 5 second.
<script language=JavaScript>
setTimeout("window.location.history.go(-1)",5000);
</script>


Regards,
---Pinal
Just a note that by automaticaly sending them back to where they came from WILL confuse them.


BUT if you insist...

The best way to know where they came from is that when they hit submit to go to the cart save the page URL to a session variable using the CGI.SCRIPT_NAME and CGI.QUERY_STRING variables.

Then you can use them on the cart page to recreate the URL the user came from.

The problem with using back is that the previous page will not be refreshed.

Personally I would just have a "Continue shopping button" and let the user control where they go.  Maybe they don't want to go back?  Maybe they want to check out.  But by you sending them back they can't checkout and so leave your site....

Just some things to cinsider.
Even so...I could have the continue shopping button take them back to the place they were. The page they are on that displays a list of items in their cart, already has links to check out or navigate throughout the website. I just didn't want users to have to remember how they navigated to where they were in the previous screen. I tried  <input type = "button" onclick = "history.go(-2)" value = "Continue Shopping"> but it doesn't refresh the page and clear previously selected information. I thought that would confuse some users.
Hi ktjamms2,

Normally you store the page they were last when you want to return them to it, you can store it in a session variable or pass it in a post or url data, relying on javascript history has issues all of its own.

Regards
Plucka
What is the Cold Fusion syntax to store the previous page in a session variable?
ASKER CERTIFIED SOLUTION
Avatar of Plucka
Plucka
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this works good Plucka,

<cfset Session.ReturnTo = CGI.SCRIPT_NAME />

Then in your next page you can do a link or auto refresh with this variable.

Ie:

<a href="#Session.ReturnTo#">Back</a>

The problem I have now is that when the session times out via:
<cflock TIMEOUT="90">(I increased the time from 30 to 90)
<cftransaction>  etc. An error occurs while evaluating #Session.ReturnTo#

Is there a drawback to not setting a TIMEOUT ?

If you pass it in the url then it will never expire, otherwise you could just increase the timeout further or change the setting in Administrator.

To pass it in a URL.

<a href="nextpage.cfm?ReturnTo=#CGI.SCRIPT_NAME#">Whatever</a>

As for the TIMEOUT, unless you need to timeout the session then dont.

The <cflock isn't timing out the session I don't believe, this is to do with the session timeout. You either don't have your session variables setup properly or they timeout too quickly.

Here's the code I normally throw in my application.cfm to setup session variables properly and make sure they expire when the browser is closed.

<cflock name="ApplicationName" throwontimeout="no" timeout="20">
      <cfapplication       name="ApplicationName"
                              sessionmanagement="Yes"
                              setclientcookies="Yes"
                              sessiontimeout="#CreateTimeSpan(0,8,0,0)#">
      
      <!--- This code kills all session variables when the browser is closed --->
      <cfif IsDefined("Cookie.CFID") AND IsDefined("Cookie.CFTOKEN")>
            <cfset cfId_local = Cookie.CFID>
            <cfset cftoken_local = Cookie.CFTOKEN>
            <cfcookie name="CFID" value="#cfId_local#">
            <cfcookie name="CFTOKEN" value="#cftoken_local#">
      </cfif>
</cflock>

This sets up session variables for 8 hours.
One other thought, if the page runs for a really long time and you don't want to worry about sessions expiring quickly do this at the top of your page.

<cfparam name="Request.ReturnTo" Default="#Session.ReturnTo#" />

Then Request.ReturnTo will be available as long as the page is open. So you can use this in your redirects.

Ie:

<a href="#Request.ReturnTo#">Back</a>

or go back after 5 seconds.

<script language=JavaScript>
    setTimeout("#Request.ReturnTo#", 5000);
</script>

<a href="nextpage.cfm?ReturnTo=#CGI.SCRIPT_NAME#">Whatever</a>


I already have the session.urltoken varriable being passed

<A HREF="nextpage.cfm?#session.urltoken#">whatever</A>

Can I also pass the ?ReturnTo=#CGI.SCRIPT_NAME# with the same link?
Yes like this :

<A HREF="nextpage.cfm?#session.urltoken#&ReturnTo=#CGI.SCRIPT_NAME#">whatever</A>
what is the advantage of killing all session variables when the browser is closed?
It prevents session hijacking.
I don't know what that means.

Anyhow, I still get an error when the page sets idle for a period of time.
I have an order page that posts to a page that enters the items in a data base and redirects to a page where the viewer can see all of their selections. I made a button to continue shopping that utilized the <a href="#Session.ReturnTo#"> and there is another button if selected will post to a page that removes the item from the data base and displays the remaining items for the user to view. When the remove item button is clicked after the page is idle for awhile an error occurs.

 I set the <cfparam name="Request.ReturnTo" Default="#Session.ReturnTo#" /> on the redirect page, the view items, page and to page it posts to (view items 2).
 
on the redirect page I have:
<meta http-equiv="refresh" content="0;URL=http://mydomain.com/page1.cfm?#&ReturnTo=#CGI.SCRIPT_NAME#">


the item view page posts to item view 2:
action="page2.cfm?#session.urltoken#&ReturnTo=#CGI.SCRIPT_NAME#"
method="post"

item view 2 posts to itself  because it processes the removal function and displays an updated view.

Both pages seem to time out if the the remove item button is clicked. Every other link works including the continue shopping that takes them back
session hijacking is where Person A logs on and creates a session.

Then they close the browser, but you don't kill the session.

Now person B logs on and starts using Person A's session variables.

They have "hijacked" the session.

In most cases the session will time out, but this can happen even unintentionally.

This is why it is a good practice to clear a session when the browser closes - because people expect that cllosing the browser gets rid of thier information.


Part of the problem with sessions is the timeout you are mentioneing above.

Yes if the page sits idle too long an error will occur.

The only way to prevent this is to test if the session exists on every page and if not then create a new one.
I've noticed that the redirect page doesn't  seem to pass the #session.urltoken#
before the redirect page the url is:
http://mydomain/formpage.cfm?CFID=12345&CFTOKEN=12345678
after the redirect the url is:
http://my domain/page1.cfm?#session.urltoken&ReturnTo=CGI.SCRIPT_NAME
on the redirect page I have:
<meta http-equiv="refresh" content="0;URL=http://mydomain.com/page1.cfm#session.urltoken#&ReturnTo=#CGI.SCRIPT_NAME#"> but the url token doesn't seem to get passed on
Youneed a ? after teh .cfm before teh #session.....
Also make sure it is enclosed in a cfoutput
ok, thanks that works....I should have seen that one.

but I need the <a href="#Session.ReturnTo#">
to also pass my #session.urltoken#

<a href="#Session.ReturnTo#&#session.urltoken#"> comes up page not found
Try this on the page :

<cfoutput>#Session.ReturnTo#&#session.urltoken#</cfoutput>

Note it is not inside a  <a href ... >

and see what prints....
likely it wants to go back to the redirect page....which I don't want and haven't set the
<cfset Session.ReturnTo = CGI.SCRIPT_NAME /> on that page.  How can I work around that?
<cfoutput>#Session.ReturnTo#&#session.urltoken#</cfoutput>

Note it is not inside a  <a href ... >

DIDN'T WORK MRICHMON ..... just displays the actual values instead of the variable
what exactly does it display?
/page.cfm&CFID=12345&CFTOKEN=12345678
OK I got the cfid to pass by reversing the code to
a href="#Session.ReturnTo#?#session.urltoken#">

but the page still seems to time out or something
The problem is that you need a ? after the .cfm as I mentioned several messages back...
Well time out is not really something we can address....
The problem with using <a href="#Session.ReturnTo#?#session.urltoken#"> appears to come from the fact that one of the pages posts to itself and the first page is a result of a redirect page. When it times out, it causes an error.
<cfparam name="Request.ReturnTo" Default="#Session.ReturnTo#" /> does not seem to work
 Is there another way around that?