Disable back button in GWT

前端 未结 5 557
旧时难觅i
旧时难觅i 2021-01-15 14:28

Is there a way to disable the Back button in a browser (basically clearing the History token stack) in GWT? Once I browse to a certain page in my application I want to make

5条回答
  •  囚心锁ツ
    2021-01-15 14:52

    Call the method below in the onModuleLoad().

     private void setupHistory() {
            final String initToken = History.getToken();
            if (initToken.length() == 0) {
                History.newItem("main");
            }
    
            // Add history listener
            HandlerRegistration historyHandlerRegistration = History.addValueChangeHandler(new ValueChangeHandler() {
                @Override
                public void onValueChange(ValueChangeEvent event) {
                    String token = event.getValue();
                    if (initToken.equals(token)) {
                        History.newItem(initToken);
                    }
                }
            });
    
            // Now that we've setup our listener, fire the initial history state.
            History.fireCurrentHistoryState();
    
            Window.addWindowClosingHandler(new ClosingHandler() {
                boolean reloading = false;
    
                @Override
                public void onWindowClosing(ClosingEvent event) {
                    if (!reloading) {
                        String userAgent = Window.Navigator.getUserAgent();
                        if (userAgent.contains("MSIE")) {
                            if (!Window.confirm("Do you really want to exit?")) {
                                reloading = true;
                                Window.Location.reload(); // For IE
                            }
                        }
                        else {
                            event.setMessage("My App"); // For other browser
                        }
                    }
                }
            });
        }
    

提交回复
热议问题