File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Check if updates were made before page unload Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Check if updates were made before page unload" Watch "Check if updates were made before page unload" New topic
Author

Check if updates were made before page unload

Efrat Bar-Nahum
Ranch Hand

Joined: Jan 19, 2006
Posts: 57
Hi,

I have a web application using the JSF framework.
Some of the pages have editable parameters & a "Set" commandButton.
If one of these parameters was changed and the set button wasn't pressed, I would like to alert the user before the page is unloaded.
If the user chooses to discard changes the new URL will be loaded.
But if not - I would like to leave the current page unchanged (without reloading it, to keep the changes).
How can I do it?

Thanks a lot,
Efrat
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
I would like to alert the user before the page is unloaded.

a javascript alert?

http://farfetched.blogspot.com/2005/03/warn-of-unsaved-changes-javascript.html


MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
rajani varma
Ranch Hand

Joined: Dec 21, 2006
Posts: 30
Hello, This can be done by using javascript. The javascript function should return a boolean value. If the function returns false, then the page will not be submitted, else it will perform the action. For eg.

In the jsf page, the onclick attribute of commandButton should return false,

"if(!confirmCancellation()){return false;}"

And the javaScript function should be,

function confirmCancellation(){

var confirmation = confirm(' Your Selection Cancels the activity');
var flag;
if(confirmation){
flag=true;
}else{
flag=false;
}
return flag;
}


Hope this helps.....

-Rajani.
Efrat Bar-Nahum
Ranch Hand

Joined: Jan 19, 2006
Posts: 57
Hi Rajani,

Thanks for your reply.
This solution sounds good, but it doesn't solve my problem.
The command button is pressed when the user wants to submit the changes.
Only if the user DID NOT press this button, and decided to leave the form, I want to alert.

did something like this:



where checkValueChanged is defined as follows:





When the user presses the OK button of the confirm dialog, the new URL is reloaded, when cancel is selected, the current URL is reloaded.
But the problem is that I don't want to reload the current page, I want to do nothing, because if the current page is reloaded I lose all the changes that were made.
How do I stop the new page from reloading without reloading the current page again?

Thanks a lot,
Efrat
rajani varma
Ranch Hand

Joined: Dec 21, 2006
Posts: 30
Hi Efrat,
In the JSF page, the command button will have both the onclick and action attributes. First the onclick method is called before the action attribute, if you click the command button. So first the javascript function is executed,


For eg. in this function,
function confirmCancellation(){

var confirmation = confirm(' Your Selection Cancels the activity');
var flag;
if(confirmation){
flag=true;

}else{
flag=false;
}
return flag;
}

This function opens an alert window. If "Yes" on alert window is clicked, then it will load the new url depending upon the value given in the "action" attribute of the command button(here the javascript function returns true, so the action attribute of command button is executed). If you click on "no" of the alert, the javascript function will return false and the page will not be submitted,(it will reload the same url without refreshing/submitting the page) and all the changes you have done will remain same. Just the alert will be closed.

This is working fine for me. In my case, the situation is, if I am in the middle of an activity and want to cancel the activity, I click on the cancel button. It will open an alert which says 'Your Selection Cancels the activity'. If I click "yes", I will be taken to the starting page. If I click "No", Then the alert window closes and I will remain in the same previous page where I was before(without submitting this page again).

Hope this should work for you also

-Rajani.
Efrat Bar-Nahum
Ranch Hand

Joined: Jan 19, 2006
Posts: 57
Hi Rajani,

Thanks for your answer.
The thing is that the command button is not pressed, so I can't use the onclick function.

To make things clearer, here's the full scenario:
1. The user changes the values in some of the configurable fields. The user can submit the changes by pressing the command button.

2. If the user submitted the changes (now the new values are written in the DB), the user can navigate to another page. No Alert will be shown.

3. If the user didn't submit the changes (meaning - the command button WAS NOT PRESSED), but he still wanted to navigated to another page (by selecting an item from the menu, navigation tree or entered a new URL manually) - This is the case that I'm talking about.
If this is the case, I would like to alert a warning message.
I know that I can use the onunload function, the thing is that I don't know how to cancel the unload (if that's what the user chooses to do in the confirm dialog).


Any idea?
Thanks,
Efrat
rajani varma
Ranch Hand

Joined: Dec 21, 2006
Posts: 30
Hello Efrat,

Sorry before this, I couldn't understand the exact situation of how the user can submit the page without clicking the command button.
Ok I had a similar situation. In my case, if the user is doing an activity type and if he is in the middle of it and clicks on any other menu item or navigation tree, then we are showing an alert to warn user with message, "Are you sure you want to cancel this activity".
This is done in the backing bean.
In your case, set a boolean variable in the backing bean,and check if the user is in the current page,(or any other condition) and set boolean to true, else false and save the state of it in the page. So now whenever any other item in the menu is clicked, the action attribute of that menu item calls a method in the backing bean. That method should check for the boolean value and return the javaScript alert, Check for this condition in all the other items action methods, which the user can click on that page, except the commandButton.
For Eg., in the jsp, each menu item will be,


In the bean,


This method will return the javascript alert without submitting the current page.

Hope this helps,

-Rajani.
 
 
subject: Check if updates were made before page unload
 
Similar Threads
How to obtain parameters from request
URL Parameters
scopes
User Input Parameters
struts2: how to set parameter?