1

I have two classes when navigating from one class to another created history. Now, I want to prompt the user by a confirm whether user wants to leave the page or not.Till now , I tried using Window.closingHandler() in gwt but its not working for backspace button and browser back button its only working on closing the entire browser or that particular page using cross. Its also working on reload.

I have also tried on Javascript and it worked perfect using onbeforeunload().

Here are the codes I used.

JAVASCRIPT:

window.onbeforeunload = function () {
  return "Are you sure you wish to leave this delightful page?";
}

And the other code in gwt:

Window.addWindowClosingHandler(new Window.ClosingHandler() {
    public void onWindowClosing(ClosingEvent event) {
        event.setMessage("Do you wanna close?");
        System.out.println("Closing...");
    }
});

I want it to be done in gwt.

Farvardin
  • 5,336
  • 5
  • 33
  • 54
Vartika
  • 1,085
  • 3
  • 17
  • 43
  • the onbeforeunload() is not working on jsni i tried it too... – Vartika Apr 25 '14 at 11:55
  • seraching for such function in gwt – Vartika Apr 25 '14 at 11:59
  • Since you're not leaving your app when using "back" `onbeforeunload` doesn't fire. One alternative would be to listen for a `History` change event in this case. – Baz Apr 25 '14 at 12:14
  • its working on html pages but not in gwt project. itried to get it on valuechangehandler() but i cant get the prompt there. – Vartika Apr 25 '14 at 12:20
  • i got a problem with it as its working twice when i called that class and when i changed the history by clicking on browserback button – Vartika Apr 25 '14 at 12:22
  • [This](https://groups.google.com/forum/#!topic/google-web-toolkit/aMDOTYGS1D0) might be relevant as well. – Baz Apr 25 '14 at 12:35
  • Have you tried overriding onStop() in activity. – Ankit Singla Dec 20 '16 at 09:25

2 Answers2

1

Have you tried with History.addValueChangeHandler which listens for changes in the browser's history stack?


-- EDIT --

Below code is working fine for Backspace key in Firefox, Chrome as well as IE9.

Note: Please test it for other browsers also and let me know if there is any issue.

    Event.addNativePreviewHandler(new Event.NativePreviewHandler() {

        @Override
        public void onPreviewNativeEvent(final NativePreviewEvent event) {
            boolean isFirefox = checkBrowser("Firefox");
            if ((isFirefox && event.getTypeInt() == Event.ONKEYPRESS)
                    || (!isFirefox && event.getTypeInt() == Event.ONKEYDOWN)) {
                if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_BACKSPACE) {
                    Element element = Element.as(event.getNativeEvent().getEventTarget());

                    String tagName = element.getTagName();

                    System.out.println(tagName);

                    if (!tagName.equalsIgnoreCase("INPUT")
                            && !tagName.equalsIgnoreCase("TEXTAREA")) {

                        boolean result = Window.confirm("Are you sure?");
                        if (!result) {
                            event.cancel();
                        }
                    }
                }
            }
        }
    });

    if (checkBrowser("Firefox")) {
        DOM.sinkEvents(RootPanel.get().getElement(), Event.ONKEYPRESS);
    } else {
        DOM.sinkEvents(RootPanel.get().getElement(), Event.ONKEYDOWN);
    }

....

private static boolean checkBrowser(String browserName) {
    return (Window.Navigator.getUserAgent().toLowerCase().indexOf(browserName.toLowerCase()) != -1);
}

-- EDIT --

Here is the code detect browser Back button also. For more info have a look at

How to know whether refresh button or browser back button is clicked in firefox

public native void onBeforeUnload()/*-{

    $wnd.onbeforeunload = function(e) {
        return 'Are you sure?';
    };
}-*/;

public void onModuleLoad() {

    onBeforeUnload();

}

screenshots (browser back button is clicked or page is refreshed)

enter image description here

enter image description here enter image description here

Please have a look at below posts:

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • yes, i tried History.addValueChangeHandler but its not working , i told u that its working twice when the first time the class is called and during the change of history also. – Vartika Apr 26 '14 at 04:59
  • braj still its not working for browser back button . otherwise ur code is useful. jst like this i tried it with jsni also. – Vartika Apr 26 '14 at 05:00
  • on constructor of class. now , i have one more question for u, how to run gwt without plugin on safari and opera browser – Vartika Apr 26 '14 at 10:00
  • As now i am facing browser compatibility problem – Vartika Apr 26 '14 at 10:00
  • 1
    You need plugin only for dev mode. Deploy your war on tomcat or any other server and test it in any browser. – Braj Apr 26 '14 at 10:01
  • we have already worked on the same function but in jsni its not working and perfectly working in simple javascript for html pages. see the first example of my ques its the same. – Vartika Apr 26 '14 at 10:29
  • I have tried it in Firefox, Chrome as well as IE9. Its working fine for me when back button is clicked or page is refreshed. Please have a look at my last edit section. – Braj Apr 26 '14 at 10:30
  • Its `$wnd.onbeforeunload` not `window.onbeforeunload` in GWT JSNI. – Braj Apr 26 '14 at 10:31
  • In what browser are you testing it? – Braj Apr 26 '14 at 10:33
  • actually its working when we close the particular page from browser but not on its back button plz re-chk – Vartika Apr 26 '14 at 10:35
  • Please tell me on which browser are you working so that I can test it. – Braj Apr 26 '14 at 10:36
1

I have got this in JSNI, But its also not working in browser back button..

public native void call()/*-{

    $wnd.onkeypress = GetChar;

     function GetChar (event)
     {
        var key = event.keyCode;

        var bb = event.target.nodeName;

             if(key==8 && bb=="BODY")
                {
                    var x= window.confirm("Are you sureyou want to leave the page");

                    if (x==true)
                            {
                                window.history.back();
                            }
                    else if(x==false)
                            {

                                return false;
                            }
                }
        }                   
}-*/;
  • onkeydown is working for both brewser but in FF on confirm cancle this call previous class ....?????????????/ –  Apr 26 '14 at 07:49
  • Thanks again for your help. I have updated post where I have handled it for `browser back button` as well ad `backspace key` for all browsers. – Braj Apr 26 '14 at 10:23
  • No I haven't use this function. I have taken inputs from this link [how can we use onbefoerunload in GWT](http://stackoverflow.com/questions/23291626/how-can-we-use-onbefoerunload-in-gwt/23295079?noredirect=1#comment35685905_23295079) asked by @user3364549 – Braj Apr 26 '14 at 10:46
  • I have chkd on opera and safari also working perfect – Vartika Apr 26 '14 at 11:33