2010-03-05 98 views
2

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

+6

如果你必须禁用后退按钮,你做错了。编写良好的Ajax工具包旨在正确处理后退按钮(以便他们将调用应用程序的回调---与单击导航链接的效果相同)。 – 2010-03-05 18:20:31

+1

有很多关于此重复:http://stackoverflow.com/questions/1864706/disable-back-button/1864709#1864709,http://stackoverflow.com/questions/961188/disable-browsers-back-按钮 – 2010-03-05 18:21:09

+0

@迈克尔:是的,仍然与“不要”的基本信息 - 应该是。 :-) @OP:请参阅http://www.useit.com/alertbox/990530.html,第1项。 – 2010-03-05 18:24:40

回答

6

你不能禁用一个按钮只是截取它,并将其返回值改变为浏览器不理解的东西。

这消除了历史:

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

不过,我建议不这样做,因为你它违背了优秀的用户界面的做法。相反,您应该找出后退按钮不会导致代码出现问题的方式。

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

这不是一个傻瓜证明的解决方案。在火狐中,我可以按下后退按钮,并且绝不会调用方法onWindowClosing。原因是我使用了History.newItem(),并且由于历史记录存在后退按钮或退格按钮,因此只需浏览浏览器历史记录即可。

所以....修复:)

1

我发现了一个方法,使GWT忽略后退按钮:只需添加historyitem x如果没有historyitem设置并做X什么。

  1. 历史的ValueChangeHandler设置historyitem上启动

    History.newItem("x") 
    
  2. 添加以下内容:

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

在index.html文件

将这个

window.open(' html页面(例如trial.html)','所需站点的名称',width ='你想要的',height ='你想要的',centerscreen = yes,menubar = no,toolbar = no,location = no , personalbar = no,directories = no,status = no,resizable = yes,dependent = no,titlebar = no,dialog = no');

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