3

I haven't this issue in the other popular browsers. In a JSP page (let's call it Page1.jsp), I click a form which goes to a different page. When I click Opera's Back button, it goes back to Page1.jsp, but no JavaScript or JSP code has refired. For example,

<!-- Inside header tag -->
<script language="JavaScript>
  alert("Does this work in Opera?");
</script>

A simple alert statement does not fire. The same goes for server-side code. However, the other popular desktop browsers don't have a problem.

How can I fix this?

Thank you for any help.

Update

I should've stated -- and I apologize for this -- that Page1.jsp contains a frame/an iframe, which is where the submit form lives. When the user submits the form, it loads a new page inside the frame/iframe. At this point, when the user clicks the browser's back button, I see that Page1.jsp is cached, but the frame/iframe reloads. This occurs in all the popular browsers except Opera.

user717236
  • 4,959
  • 19
  • 66
  • 102
  • Can you post a demo somewhere (JSFiddle perhaps)? I suspect it has to do with Opera's iframe security, but it would help greatly to see what you're talking about. – webinista Apr 18 '12 at 21:00

2 Answers2

3

Actually, most browsers behave like this, returning the browser to the state you left it rather than reloading a visited page 'from scratch'. It is usually a good thing, makes navigating quicker.

The browsers that 'save state' do so on unload, to override it you can replace the unload handler with one of your own-

<script>
alert('loaded');

onunload=function(){ return true; }
</script>

It is possible that a user can have some kind of autosave set on his browser to remember the state of forms between visits- for this, you may need to manually set the form fields to their defaults in the unload handler.

kennebec
  • 102,654
  • 32
  • 106
  • 127
  • Thank you very much for your help. Actually, and I'll modify the question, the form is in a frame/iframe, which is inside Page1.jsp. So, when the user clicks the browser's back button, I see that Page1.jsp remains unchanged, but the contents of the frame/iframe is reloaded. This happens on all popular browsers except Opera. – user717236 Apr 12 '12 at 14:49
1

@kennebec: Thank you very much. Because of your answer, I was able to do some more digging and I found a stackoverflow question here and an Opera article here, which addresses the problem:

history.navigationMode = "compatible"; // Opera only property
$(document).ready(function() {
  alert("test");
});

According to this site, navigationMode is only supported by Opera browsers. So, this works perfectly and I don't have to script any unload events. I looked up pageshow/pagehide, but it doesn't appear to be fully cross-browser compliant, and HTML5 has onpageshow/onpagehide, but I need to be cross-browser complaint and support legacy browsers.

Community
  • 1
  • 1
user717236
  • 4,959
  • 19
  • 66
  • 102