1

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?

4
  • 1
    Bad usability and frames and a flash app in the same question? Commented Apr 17, 2009 at 1:03
  • And why are you using JavaScript for basic page navigation? As Andrew Duffy has mentioned, that's what the "target" attribute is for. Commented Apr 17, 2009 at 2:47
  • @alex -- Just because frames and flash are generally considered bad design, are over used, and are used incorrectly, it doesn't mean that there aren't legitimate uses for them. @Calvin -- the reason javascript is necessary for basic navigation, is because when using Response.Redirect from within my code, there is no way to specify a target since that is all handled by the client. So writing a script to the page that does the redirect for me allows me to specify a target. Commented Apr 17, 2009 at 14:34
  • I would only call them bad design because of their accessibility issues. For a start... Commented Apr 18, 2009 at 4:26

3 Answers 3

11

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' );
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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.

2 Comments

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?
I think it's cos they consider target to be a behaviour, so it should be incorporated into JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.