0

Page 1 contains a dynamic form, page 2 is a review of submitted data. I have the following constraints :

  • no beforeunload warning when submitting page 1 ;
  • beforeunload warning when refreshing page 1 ;
  • no reloading of page 1 when issuing history.back() from page 2.

I tried to play with window.onbeforeunload and $(window).unload() (in page 1) but I can't seem to get the expected result.

Is this even possible, or are these constraints definitely contradictory (I mean, for a reasonable number of JS lines…) ?

Update :

I removed constraint 3 because according to some reading it deserves its own question.

Community
  • 1
  • 1
Skippy le Grand Gourou
  • 6,976
  • 4
  • 60
  • 76

1 Answers1

2

You should be able to use something like this for parts 1 and 2, not sure what you mean with part 3 though

"use strict";
var isSubmit = false
document.querySelector('button').addEventListener('click', e => {
  isSubmit = true
  window.location.reload()
})

window.onbeforeunload = e => {
  if (!isSubmit) {
    return "Are you sure you want to leave?"
  }
}
<button>Submit</button>
jmcgriz
  • 2,819
  • 1
  • 9
  • 11
  • Indeed it does the trick for constraints 1 and 2, thanks, but not for constraint 3. I'll try to rephrase : unload shouldn't be triggered when going back from page 2 to page 1. It seems to be the default behaviour, but when the JS code includes `onbeforeunload` or `unload` page 1 will be refreshed when coming from page 2. – Skippy le Grand Gourou May 22 '16 at 15:13
  • [This answer of another question](http://stackoverflow.com/a/2639165/812102) is related — in other words, I need bfcache to be enabled even though I use `onbeforeunload` and `unload`. – Skippy le Grand Gourou May 22 '16 at 15:31
  • I'll accept this answer and edit the question to remove constraint 3, which I will ask about in another question, since it seems to be too specific. – Skippy le Grand Gourou May 22 '16 at 15:49