In an old web development platform I have used (several years back), this was solved using a built in function. The idea was that the server maintained some sort of internal collection of urls and mapped those to simple numbers. When you added a new url to the collection, the server would first check if that exact url was already present and if so just return the associated number. If the url you were adding was new, it would be added and a new number would be returned.
A couple of samples to illustrate... Let's assume the user starts out at page:
http://foo.bar.com/search/
I'll referr to this page a "Page 1". After adding the criteria to search for, the user will be shown the reuslts on page:
http://foo.bar.com/search/results?jobTitle=CEO&etc&etc
I'll call this "Page 2". On this page the user can click open a specific result and get to page:
http://foo.bar.com/search/results/12313
I'll call that "Page 3".
To have the "back to previous" functionality for pages 2 and 3, they would need to be opened with one additional query parameter. When page 1 is generating the url for page 2, the server would call the utility method SaveUrl()
(on some suitable class) and append the result to the querystring so the url for page 2 becomes:
http://foo.bar.com/search/results?jobTitle=CEO&etc&etc&return_url=1
Notice the new return_url=1
query parameter. Page 2 would then use the corresponding GetReturnUrl(1)
method to fetch the url to use in the "back to previous" link. This call would bring back the url that was added previously, which would be http://foo.bar.com/search/
.
Going forward from page 2 to page 3 would also involve adding a new querystring parameter using the same SaveUrl()
. The new url to page 3 would then be:
http://foo.bar.com/search/results/12313?return_url=2
On page 3, the "back to previous" page url would be fetched using GetReturnUrl(2)
, which would give back http://foo.bar.com/search/results?jobTitle=CEO&etc&etc&return_url=1
.
So this system works by storing the url of the current page and supplying that in the url to the next page. And since the url for the current page contains a reference to the current page's previous page in the form of the query parameter, the back to previous trail is maintained.
I thought I would explain this because I think it's a smart way to handle the back to previous trail. I'm not claiming that it's any sort of industry standard or best practise though...
Edit: Some thoughts and considerations regarding this approach.
Pros:
- Easy way to store and retrieve urls without causing an ever longer querystring
Cons:
- Works as long as the url contains all the necessary state to go back.
- Depending on the implementation details (global url collection? user specific url collection in Session? persist in Sql server?) it might cause issues in web farms (all servers need to agree on what url is stored as "1").
- Might cause memory issues in high traffic sites (but that also depend on the specific implementation)