File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Warn user when have unsaved data

 
M. LaBarre
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything I have read about about how to accomplish this uses the onBeforeUnload event. But this event is called when a page refreshes as well as when the page is unloading.

I can easily use my backing bean to determine when the user needs to save but what I can't figure out is how to tell the difference before a page refresh vs a page unload.

Is there some way I can determine the current page == target page from the Faces Context?

I envision something like this

window.onbeforeunload = function() {
var needToSave = ${pc_myform};
if (needToSave == true) {
check if target page != current page)
alert("you need to save")
return false;
}
}
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so why would the page/form refresh while still being "dirty"? I can see the user clicking the refresh button, but warn the user then too, because I would assume that the refresh could refresh the page to show the older data values, and therefore lose your unsaved data.

Meaning I am thinking the way it is operating is the way you should want it. But I could be clueless, it wouldn't be the first time.

Mark
 
M. LaBarre
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try to explain this better.

I have a form where the user can add favorite forms to their profile. The users insist on having a save and cancel button, so I can't persist the data immediately. Each time they add a new favorite, or remove one, the screen is updated to display this change(so the page gets refreshed) but the data is not saved until they click on the save button or the changes are reverted back if they click on the cancel button. But there are other links the user can click on that will navagate away from the page.

This gets a but more complicated in that there is a parent page, that holds several iframes with other pages. And it's the iframe pages where the data can be updated.

So the onBeforeUnload event would be the best option if I could only determine what the current page is and what the target page is.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic