Wedge
Public area => The Pub => Off-topic => Topic started by: PantsManUK on October 26th, 2011, 10:23 AM
-
Bit of a technical question for the proper devs out there (I get by, but I'm not a dev by any stretch)...
At work we use MantisBT, and we have a number of users who either because of stupidity or sheer bloody mindedness don't remember that if they use the browser "Back" and "Forward" button(s) whilst filling in the new issue form, the form loses any information they typed. So, I'm wondering how feasible it would be to dury-rig MantisBT into keeping the form contents for these people?
Now, actual question is, since these are the browser controls at work, is there a way of doing the saving client-side/in-browser, rather than server-side?
-
Sure it's possible. How rugged it'll be is another matter entirely.
In recent browsers, there is the notion of localStorage, a browser-side storage area that is bigger than cookies, not shared with the server unless JS instructs it to be (via normal HTTP requests etc). I won't get into the details of implementing that part, since it's not like there aren't tutorials throughout the web on the different browser implementations including some mini libraries that abstract that stuff away.
The real problem that you need to solve is the mechanism by which you're triggering a 'save' to be made and when to clear that save, as it were.
There are two principle ways of triggering a save, you can either do it from a JavaScript setInterval call, which will run every x seconds and you pass a function to it to indicate what it should do every x seconds. It's pretty trivial at that point to scrape the elements on the page and dump them into a localStorage container.
Off hand I'm not sure how you'd trigger firing the reload-from-localStorage when on the next page, perhaps run a setInterval again, see if the form's empty and whether there's anything in localStorage.
As for pruning, in the 'thank you for submitting' page or whatever screen appears after saving a ticket, just insert a line to clear localStorage.
There are two other solutions that come to mind.
1. Use a browser that doesn't have this problem. I don't seem to have any trouble in Chrome with navigating back/forward on pages with forms in.
2. Add an event to the onBeforeUnload handler, which checks to see if there's any content in the page and warns users about leaving the page. It need only be as complex as:
<script>
window.onbeforeunload = function beforeUnload()
{
return (document.getElementById('my_textarea').innerHTML == '') || confirm('If you leave this page, your changes will be lost.');
}
</script>
Of course that relies on MantisBT not putting any other events in onbeforeunload (if so you'll have to do more complex things!) and expand the checks to anything else you care to use, but the principle is sound.
-
"+1" and "Like" on that, thank you sir. I'm going to go with the "don't be a tool and leave the page without saving" option, I think (assuming Mantis isn't hooking it itself, but the little I do know about MantisBT suggests they aren't (they are pretty much "PHP and nowt else...").
-
Yeah, that approach is probably for the best; there is only so much safety-net you can string out under users...