【问题标题】:Disable back button in GWT禁用 GWT 中的后退按钮
【发布时间】:2010-03-05 18:18:58
【问题描述】:

有没有办法在 GWT 中禁用浏览器中的后退按钮(基本上清除历史令牌堆栈)?一旦我浏览到应用程序中的某个页面,我想确保用户不能使用后退按钮返回,而只能使用页面上的链接来导航站点。

【问题讨论】:

标签: java javascript gwt


【解决方案1】:

您不能禁用按钮,只是拦截它并将其返回更改为浏览器无法理解的内容。

这会删除历史记录:

 Window.addWindowClosingHandler(new ClosingHandler() {
     @Override
      public void onWindowClosing(ClosingEvent event) {
      event.setMessage("My program");
      }
    }); 

要了解它,请参阅:http://groups.google.com/group/google-web-toolkit/browse_thread/thread/8b2a7ddad5a47af8/154ec7934eb6be42?lnk=gst&q=disable+back+button#154ec7934eb6be42

但是,我建议不要这样做,因为这违反了良好的 UI 实践。相反,您应该找出一种方法,使后退按钮不会导致您的代码出现问题。

【讨论】:

    【解决方案2】:

    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
                        }
                    }
                }
            });
        }
    

    【讨论】:

      【解决方案3】:

      我找到了一种让 GWT 忽略后退按钮的方法:如果没有设置历史项,只需添加历史项 x 并且对 x 不执行任何操作。

      1. 在启动时设置一个历史项目

        History.newItem("x")
        
      2. 在 History 的 ValueChangeHandler 中添加以下内容:

        String historyToken = event.getValue();
        if (!historyToken.equals("x"))
          History.newItem("x");
        

      【讨论】:

        【解决方案4】:
        Window.addWindowClosingHandler(new ClosingHandler() {
            @Override
            public void onWindowClosing(ClosingEvent event) {
                event.setMessage("My program");
            }
        });
        

        这不是一个万无一失的解决方案。在火狐中,我可以按后退按钮,并且永远不会调用 onWindowClosing 方法。原因是我使用了History.newItem(),并且由于存在历史记录,因此后退按钮或退格按钮只需在浏览器历史记录中导航。

        所以....解决这个问题:)

        【讨论】:

          【解决方案5】:

          把它放在你的index.html 文件中:

          window.open('html page(For example trial.html)', 'Name of the desired site', width='whatever you want',height='whatever you want', centerscreen=yes, menubar=no,toolbar=no,location=no, personalbar=no, directories=no,status=no, resizable=yes, dependent=no, titlebar=no,dialog=no');

          【讨论】:

            猜你喜欢
            • 2017-11-16
            • 1970-01-01
            • 1970-01-01
            • 2011-02-17
            • 1970-01-01
            • 1970-01-01
            • 2018-02-21
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多