personaelit
personaelit

Reputation: 1653

Remove/Disable javascript when using browser's back button

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?

Upvotes: 1

Views: 1782

Answers (3)

Andrew Duffy
Andrew Duffy

Reputation: 6938

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.

Upvotes: 2

bobince
bobince

Reputation: 536695

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.

Upvotes: 2

Peter Bailey
Peter Bailey

Reputation: 105916

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' );

Upvotes: 11

Related Questions