1

First off, before the flames start, I do know that trying to hinder the back button in the browser is a dumb idea. I would not try to do this, but my business partners are very insistent on it. We are porting an existing .exe to the web and their definition of this is 'run it in a browser' and not "make it a web site". So, fact that it's a bad idea (I agree), here's the question:

Is there a way to ignore or trick the GWT PlaceController / History manager mechanisms so that when the back button is pressed, it just stays on the same page? I have used Window.addWindowClosingHandler to add a handler which will prompt the user if they want to leave the page and overriden the newItem() method of the defaultHistorian so that no history is tracked at all, but this isn't quite what the business people want.

What they'd like is to just ignore the back button so that nothing happens when it is clicked. If anyone knows how to do this with GWT, I'd be very grateful.

And I"ve done a lot of google searching and haven't found anything exactly like this question. At least, not close enough to help.

I was able to get GWT to not accumulate any history so that when the user presses the BACK button, they cause an onWindowClosing event to happen and the Browser will prompt them if they want to stay or leave. This will accomplish the goal of not allowing the BACK button to take them back, but it's a bit Draconian. This code does that:

class TvHistorian extends PlaceHistoryHandler.DefaultHistorian
{
  @Override
  public void newItem(String token, boolean issueEvent) {
    // don't do anything - this will prevent history from accumulating
  }
}

final PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper, new TvHistorian());

I've tried a bunch of stuff including extending the PlaceController.goTo() to save the "lastNormalFlowPlace". Then, I tried to override the History.onValueChange to use this saved Place if it was different than what the event specified. But I think I missed some subtlety because that didn't work as expected.

With the above exception, my code looks almost exactly like what is documented here: http://www.gwtproject.org/doc/latest/DevGuideMvpActivitiesAndPlaces.html#Putting_it_all_together

Chris
  • 73
  • 1
  • 9
  • Is it ok to prompt user when back button is clicked? – Braj Apr 27 '14 at 18:23
  • @Braj No, that's the issue. I can get it to prompt the user whenever they click the back button. But my business partners would rather it silently just remain on the current "page"/GWT screen. What I want is to only navigate if `PlaceController.goTo(Place)` gets called and to cancel/ignore any other navigation attempts. – Chris Apr 28 '14 at 17:10
  • Have you tried `History#ValueChangeHandler` as I suggested in my post? – Braj Apr 28 '14 at 17:39
  • @Braj I tried several variations with a ValueChangeHandler, but either my initial page wouldn't load at all, or they didn't help me stay on the same page (because the page that BACK takes you to was an exception for forward navigation so BACK still worked). – Chris Apr 29 '14 at 12:33
  • Please can your share your code. – Braj Apr 29 '14 at 12:34
  • Have you tried sample code that I have posted? – Braj Apr 29 '14 at 12:41
  • @Braj I tried your code "as is" and it didn't work. Also, since I'd have to add all forward navigation as exceptions, that would also allow them to work as backwards navs, right? To be honest, I don't see how your code sample would prevent BACK from functioning... – Chris Apr 29 '14 at 14:02
  • Where have you added my code? Add the code in the beginning of the `onModuleLoad()` method – Braj Apr 29 '14 at 15:52
  • I had a meeting with my business partners this morning and they agreed to live with the warning on Back so long as it consistently pops up whenever a user clicks Back. So I'm not going to spend any more time on this issue. I don't feel I can accept the answer as I couldn't get it work for me (and yes, I put it in my onModuleLoad()). Thanks for the help. – Chris Apr 29 '14 at 17:21

1 Answers1

0

I have already posted an answer in the same context.

Please have a look at below links:

Try with any option:

  • History.addValueChangeHandler
  • WindowClosingHandler
  • Event.addNativePreviewHandler
  • $wnd.onbeforeunload

--EDIT--

Sample code: (It's working perfectly fine in Firefox, Chrome as well as IE9)

Note: add below code in the beginning of the onModuleLoad() method.

    final String initToken = "Place";

    History.addValueChangeHandler(new ValueChangeHandler<String>() {

        @Override
        public void onValueChange(ValueChangeEvent<String> event) {
            String token = event.getValue();
            if (!initToken.equalsIgnoreCase(token)) {
                History.newItem(initToken);
            }
        }
    });

    // fire the initial history state.
    History.fireCurrentHistoryState();

Note: add checks for other allowed history tokens.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Note: the `__gwt_historyFrame` is only used in IE6/IE7, i.e. it should no longer be needed in GWT 2.6 with the default `user.agent`, and will be completely obsolete and useless in GWT 2.7. – Thomas Broyer Apr 27 '14 at 21:37
  • Thank you, but I've already read 2 of the three links and it's not quite what I want. All of those prompt the user if they want to leave the page when they go back. My business people don't want to prompt -- they want to just ignore the request and stay on the same page. In another (JSP-based) "site" they used the following: `//disable back button function noback(){ if (/*@cc_on @*//*@if (@_win32)!/*@end @*/false){;} else {window.onunload = function(){};} window.history.go(1); } noback();` But I'm not sure how to translate this to GWT. – Chris Apr 28 '14 at 11:49
  • @Braj - not having a good morning, forgot to tag you in previous comment. – Chris Apr 28 '14 at 12:00