简体   繁体   中英

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?

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM