Reputation: 12762
I'm trying to find a clean solution to working with the browser history in the most effective way. (I'm using GWT, but this question is really more general than that.)
Here's my situation (I would think it's pretty standard):
I have a web application that has several different pages/places/locations (whatever you want to call it), which I display in response to changes to the browser history. Beyond the usual "Home", "Features", "Contact", etc. which are mostly static HTML pages, there is a "User" section, where people can log into their user accounts and (let's call it) a "Project" section, where users can work on their projects.
So, now I simply use local links called #Home, #Features, #Contact, etc., as well as #User, #Project to get to the different pages. And everything is fine, except the following scenario:
If someone opens the link #Project, for example, that person is shown a project login dialog. This login dialog has a cancel button, which I would like to implement simply by calling the browser's back button from my app (easy enough). The reason I would like to do this, is two-fold:
At this point everything is great, except when the user initially goes to this login dialog via a direct bookmark link to #Project. Because then, if I simply have cancel = back, the user is sent away from the page entirely back to his browser's start-page or wherever he was before. So, in this case, I do need to link "forward" to #Home.
Now, I've tried to think of several ways to fix this and have come up with a couple of solutions, but none seem very desirable to me, but let me share them anyways to maybe stir up some creativity:
It seems like this should be a very common problem and I am hoping that one of you can point me to a more useful direction than my thoughts have so far.
Upvotes: 2
Views: 2283
Reputation: 41100
Before your user can access #LOGIN dialog, your entry point class is executed. In this class you can remember the last known page. In Login activity/presenter you can add a browser history event handler, which checks if the last known place is null or not. If null, send a user to the #HOME page.
You don't need to ask the browser which places in your app a user has visited. You can remember the entire history of each session, if you want to: i.e. [#HOME, #LOGIN, #FEATURES, #HOME].
Upvotes: 1
Reputation: 7174
How about you have a Queue<String> historyQueue;
When the page loads for the first time, i.e. onModuleLoad()
you initalize the Queue
.
When you capture a history event, check if its a back or a new history token.
If its a back, you pop it out from the queue, if its a new one you add it to the queue. This way, the cancel button is just a token = historyQueue.pop()
and check if the token is null. If it is, then like you said back = cancel
, so just do the appropriate thing.
Upvotes: 0
Reputation: 160073
window.history
has a length
attribute that will show you how many entries are in the history for the current tab. It is unfortunately not filterable (so there is no way to say window.history.localURLs.length
).
If you are doing this almost entirely on the client side (i. e. partial updates, very few full-page loads, using hashes or the history.(push|pop)State
API) then you may want to look into incorporating a client-side routing framework into your application to avoid re-inventing the wheel.
Upvotes: 1