-1

My app has a use case where there is a page where the user can edit data and press save or interrupt editing and make a search or click links. The framework is kind of odd and uses javascript to submit a form for every link that is clicked so we ended up with a mishmash of javascript and java hacks the facilitate the dialogs and checks for unsaved data which became messy and I had to ask about it:

How to exclude components from javascript onkeydown

https://stackoverflow.com/questions/12525020/why-does-this-code-create-a-loop

I either got dialogs that never ended, pressing "yes" to "You have unsaved data. Do you want to comtinue?" made a loop to another same dialog and when I tried to fix it nothing works.

Now the program works on the screen but the solution is messy and if there is another bug or we notice that my solution affect other component there will be trouble.

So I'm asking if you know a plain Java way to check for unsaved session data? What happens is a form is presented to the user and clicking a link when data has been edited should present a dialog thatwarns that data is unsaved. I think doing it without javascript is a better solution, can you comment and/or help me here?

Community
  • 1
  • 1
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • 2
    [It's not that hard with JS/jQuery](http://stackoverflow.com/questions/8259090/how-to-detect-unsaved-data-in-form-when-user-leaves-the-page/8259403#8259403). – BalusC Sep 28 '12 at 14:11
  • @BalusC That looks interesting. Problem is I've also got a searchbox on the page that should not trigger the dialog from searches entered. So I think I must exclude the searchbox from the elements that generate the dialog. I was thinking that if I can't do it in Java then it's better to have a plain JS / JQuery solution that doesn't have to make submit to show the dialog. – Niklas Rosencrantz Sep 28 '12 at 14:47
  • 1
    Just finetune the jQuery selector accordingly. It was merely a kickoff example. As to Java versus JS, I hope that you really understand that Java runs in webserver, not in webbrowser? So before you can get Java to run, you have to send a HTTP request. Sending a HTTP request during `onbeforeunload` is not reliably possible across browsers. – BalusC Sep 28 '12 at 14:51
  • @BalusC Than you for the comment. Yes, the way we handle it now is with the web / app server (websphere) that handles a dialog in a jspf file but I thought it could be solved either entirely with Java or entirely with Javascript because the logic now is very difficult to follow when it uses a hidden form variable for an `isDirty` flag to see if the data is unsaved. It's a combination currently and I think that there should be some "official" solution when I felt that I was reinvaenting the wheel. I can post code from my "solution" here later on. – Niklas Rosencrantz Sep 29 '12 at 10:54

1 Answers1

2

No.

The user enters data in their browser; there is no way for the server to know this unless you submit it.

David Grant
  • 13,929
  • 3
  • 57
  • 63
  • It is submitted. That's what goofy, the form is submitted when the user clicks a links so the dialogs actually appear between calls to the Java. I apologize if this became difficult to understand, it's my strangest use case for some time. – Niklas Rosencrantz Sep 28 '12 at 14:49