3

I'm using Primfaces Layout component in my pages and I'm not using Redirection in navigation when user clicks on the Menu links.

i.e.,I'm NOT using faces-redirect?true and URL doesn't change when user navigates.

I want to display a Confirmation Dialog before user navigates away from current view, like

“Are you sure you want to navigate away from this page?”

So once user clicks on the OK then it should forward to other page or else it should stay in same page if user clicks on CANCEL.

One more constraint is I want this functionality for only few selected pages not all of them.

What is the best approach to deal with this problem?

Primefaces Version: 3.5
JSF Version: 2.1.13
All my ManagedBeans are in @ViewScoped

Kishor Prakash
  • 8,011
  • 12
  • 61
  • 92
  • When you say, you want this only for selected pages, does it mean: I want ON A SELECTION of pages, that the dialog on that pages appears. or does it mean: I only want that dialog appear, when I browse away TO selected other pages? In the first case, stick with javascript and see the article http://stackoverflow.com/questions/12132060/confirm-on-window-onbeforeunload – L-Ray Jan 07 '14 at 11:06
  • @L-Ray : Consider I have 4 Pages Home,Register,Feedback and Contact. I want the dialog to fire when user is in Register page and try to navigate to any other page. – Kishor Prakash Jan 07 '14 at 11:27
  • If you're dealing only with the navigation provided by your links, just show a primefaces panel, which will provide a button wich acts like a link in the case of `OK`, and the `CANCEL` button will just hide the panel. I see nothing more here. Other case would be if you want to manage the time when user closes the tab, you'll need to handle page unload event as @L-Ray suggests. – Aritz Jan 07 '14 at 11:57
  • @XtremeBiker: Hey thanks for reply, is there any other way to do it without touching or altering the Menu links code? may be an event which behaves like opposite of javax.faces.event.PreRenderViewEvent ? – Kishor Prakash Jan 07 '14 at 12:09
  • Don't think it's possible not changing them. If you navigate to other page, you're already sending a GET request for that, there's no confirmation needed for that. You definitelly need to use your panel before that GET happens. – Aritz Jan 07 '14 at 12:18
  • @XtremeBiker : Its actually a POST request, as I mentioned in question I'm NOT using `faces-redirect=true`. So I thought it might be possible. – Kishor Prakash Jan 07 '14 at 12:24
  • Can you post your current code please? – Aritz Jan 07 '14 at 12:35
  • Hi @KishorP, got any further with the problem described? – L-Ray Jan 09 '14 at 12:34
  • Hi Kishor, did you got solution?, i need the way to show dialog when user change the view, thanks – ankush yadav Apr 15 '16 at 07:19

1 Answers1

0

If you want to try solving this problem from javascript-side, either go with

window.onbeforeunload = function(e) {
    return 'Dialog text here.';
};    

and see the dialog independent from the link being clicked (see https://stackoverflow.com/a/12132076/1269441). or try adding

onclick="if (!confirm('Your dialog-text here')) {return false;}" 

to a selection of links you would like to have the dialog shown.

Community
  • 1
  • 1
L-Ray
  • 1,637
  • 1
  • 16
  • 29
  • Hey L-Ray.., No this method is not working.. I'm using FORWARD not REDIRECT so URL won't refresh. – Kishor Prakash Jan 23 '14 at 07:18
  • The `window.onbeforeunload` works on my Firefox 26.0, so does the `onclick`-method below. The unload works independent from Forward or Redirect (at least on the upper described browser). – L-Ray Jan 23 '14 at 09:57