tags:

views:

321

answers:

3

I have a page that consists of two frames: one being a flash app, and one being an aspx page with a user control that contains a grid with several "add to cart" buttons. When the user clicks a button I would like to reload the whole window with a shopping cart page.

I found out how to do so here quite nicely by avoiding response.redirect, and using response.write to insert some javascript that redirects. The problem is that when the user hits the back button from the shopping cart page, the script executes again, effectively canceling out the back button. (bad usability)

So my question is, is there a way to detect if the user arrived at the page via the back button and avoid executing the script?

+5  A: 

Change your javascript to replace the current URL instead of just setting it

// Creates browser history
top.location = 'http://www.stackoverflow.com'

// Doesn't create browser history
top.location.replace( 'http://www.stackoverflow.com' );
Peter Bailey
bingo bango, thanks man.
Jim
+2  A: 

When the user clicks a button I would like to reload the whole window with a shopping cart page.

You should be using the Post-Redirect-Get model.

bobince
+2  A: 

You can do this easily without using script by making the buttons submit buttons in a form with a target attribute of _top. Note that the target attribute will not validate as HTML4 or XHTML.

If you are using a POST request to submit to the shopping cart (a good idea) then the Post-Redirect-Get idiom mentioned by bobince will strengthen your application further - the user won't be prompted to resubmit the form if they use the back or forward buttons to get to your shopping cart page.

Andrew Duffy
That's strange that the target attribute is being deprecated. Frames are almost always a bad idea, but it doesn't make sense to keep frames/iframes while removing the target attribute, unless they're also getting rid of frames. Do you know their reasoning for this?
Calvin
I think it's cos they consider target to be a behaviour, so it should be incorporated into JavaScript.
alex