简体   繁体   中英

Prevent user from changing the URL in Browser on Back Button

Objective: Their is a editable form in my WebApplication. I want to provide a Confirm Navigation popup to user on page change, if the form is in editable mode, so as to save the data of the form.

Problem: If the form is in editable mode and user hits navigation back button or backspace on the body, webapplication moves to previous history page without any Confirm Navigation.

I an able to provide Confirm Reload and Confirm Close on reload and closing event of the window respectively.

Is their any way to prevent the user from changing the url so as to prevent data loss of the form.

NOTE: Can use only JAVA, GWT and JAVASCRIPT.

If you are using Activities and Places design pattern, then every Activity comes with a method mayStop(). If it returns null, no confirmation is asked when a user moves to a different Place.

You can add a check to this method to see if a View is in editing mode, and return a text for your warning message if it is.

If you are not using Activity for your presenters, you should add a ValueChangeHandler to a History object in your entry point class, as described here:

http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsHistory.html

See this related question on StackOverflow .

You should probably be using javascript to do this; you want to listen for certain window events via javascript, and then fire a callback to pop up your "Confirm Navigation" prompt when those events take place.

You cannot prevent the URL from changing, but you can react to the change and prompt the user.

If you're using com.google.gwt.user.client.History directly it's a bit complex because you have to store some global state that will be used by your history's ValueChangeHandler . You'll also have to register a Window.ClosingHandler and call setMessage on the event, for the case the user is exiting your application (navigating away or closing the window/tab).

It's much easier when using Places , as there's a PlaceChangeRequestEvent being dispatched. If you call setWarning on it, then the user will be prompted whether he wants to navigate, and only if he accepts will a PlaceChangeEvent be dispatched (remember: the URL will still have changed when using the browser's back button, so there'll be desynchronization between the current place and the current URL… until the next navigation). Places takes into account the unloading of your app, so you have nothing special to do.

And in case you're using Activities , the Activity 's mayStop is called when a PlaceChangeRequestEvent is dispatched, and the value you returned is set as the message of the event if non-null.

If you want more details as to why it's not possible to prevent the URL change, see: https://code.google.com/p/google-web-toolkit/issues/detail?id=6726

(note: you could overwrite the URL, but then you'd lose the "forward history", and likely annoy users)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM