• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Check if updates were made before page unload

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic