Java 禁用GWT中的后退按钮
有没有办法在GWT中禁用浏览器中的后退按钮(基本上清除历史标记堆栈)?一旦我浏览到应用程序中的某个页面,我想确保用户不能使用“后退”按钮返回,而只能使用页面上的链接来浏览站点 您不能禁用按钮,只需截取按钮并将其返回更改为浏览器无法理解的内容即可 这将删除历史记录:Java 禁用GWT中的后退按钮,java,javascript,gwt,Java,Javascript,Gwt,有没有办法在GWT中禁用浏览器中的后退按钮(基本上清除历史标记堆栈)?一旦我浏览到应用程序中的某个页面,我想确保用户不能使用“后退”按钮返回,而只能使用页面上的链接来浏览站点 您不能禁用按钮,只需截取按钮并将其返回更改为浏览器无法理解的内容即可 这将删除历史记录: Window.addWindowClosingHandler(new ClosingHandler() { @Override public void onWindowClosing(ClosingEvent
Window.addWindowClosingHandler(new ClosingHandler() {
@Override
public void onWindowClosing(ClosingEvent event) {
event.setMessage("My program");
}
});
要了解它,请参阅:
但是,我建议不要这样做,因为您的it违背了良好的UI实践。相反,您应该找出一种方法,使“后退”按钮不会导致代码出现问题
Window.addWindowClosingHandler(new ClosingHandler() {
@Override
public void onWindowClosing(ClosingEvent event) {
event.setMessage("My program");
}
});
这不是一个万无一失的解决方案。在fire fox中,我可以按下后退按钮,并且永远不会调用onWindowClosing
方法。原因是我使用了History.newItem()
,由于历史记录存在,后退按钮或退格按钮只是在浏览器历史记录中导航
所以…修复:)我找到了一种让GWT忽略后退按钮的方法:如果没有设置historyitem,只需添加historyitem x,而对x不做任何操作
History.newItem("x")
String historyToken = event.getValue();
if (!historyToken.equals("x"))
History.newItem("x");
将其放入
index.html
文件:
window.open('html页面(例如trial.html)','所需站点名称',width='which you want',height='which you want',centerscreen=yes,menubar=no,toolbar=no,location=no,
personalbar=no,directories=no,status=no,resizable=yes,dependent=no,titlebar=no,dialog=no')代码>在onModuleLoad()
中调用下面的方法
如果您必须禁用“后退”按钮,则说明您做错了。编写良好的Ajax工具包设计用于正确处理后退按钮(这样它们将调用应用程序的回调——与单击导航链接的效果相同)。关于这一点,有很多重复:,@Michael:Yep,但仍然带有“不要”的基本信息,这是应该的。:-)@OP:见,第1项。
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
}
}
}
});
}