简体   繁体   English

在gwt中提示用户退格和浏览器后退按钮

[英]prompt user on backspace and browser backbutton in gwt

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. 现在,我想通过确认用户是否要离开页面来提示用户。直到现在,我尝试在gwt使用Window.closingHandler() ,但不适用于退格按钮和浏览器后退按钮,仅在关闭时有效整个浏览器或使用交叉的特定页面。 Its also working on reload. 它也在重新加载。

I have also tried on Javascript and it worked perfect using onbeforeunload() . 我也尝试过Javascript,使用onbeforeunload()可以完美地工作。

Here are the codes I used. 这是我使用的代码。

JAVASCRIPT: JAVASCRIPT:

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

And the other code in gwt: 还有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 . 我希望在gwt完成它。

Have you tried with History.addValueChangeHandler which listens for changes in the browser's history stack? 您是否尝试过使用History.addValueChangeHandler侦听浏览器的历史记录堆栈中的更改?


-- EDIT -- -编辑-

Below code is working fine for Backspace key in Firefox , Chrome as well as IE9 . 下面的代码对于FirefoxChromeIE9 Backspace key正常工作。

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 如何知道在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) 屏幕截图(单击浏览器后退按钮或刷新页面)

在此处输入图片说明

在此处输入图片说明在此处输入图片说明

Please have a look at below posts: 请看以下帖子:

I have got this in JSNI, But its also not working in browser back button.. 我已经在JSNI中找到了它,但是在浏览器后退按钮中也无法正常工作。

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;
                            }
                }
        }                   
}-*/;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM