.. a quick solution
Updated
Today I had a small task assigned to me to prevent a user from navigating away from a web form when there are unsaved changes. As it was a rather extensive form (and old code) I had the feeling that it could be quite an extensive task.Yes, I know this is more of a beginner's level blogpost, but maybe someone can use it. Otherwise burn me down to the ground in the comments.
General idea
A simple solution came to mind:- do this on the client
- in comes JavaScript and yep, here comes jQuery!
- have a page wide boolean isDirty that starts as 'false'
- when an input control is changed, make isDirty 'true'
- when user tries to leave the page, check the value of isDirty
- when isDirty is 'true' show confirmation dialog
- if the user cancels stay on the page
- if the user ignores then proceed onwards
Let's implement this!
Steps 1 and 2 are easy to take. We will just include the jQuery library in the page and start a block of JavaScript.Also simple is step 3.
var isDirty = false;
Step 6,7 and 8 can be wrapped in a simple function:
function IsPageDirty() { if (isDirty) { var ignoreChanges = confirm("Unsaved changes. Proceed anyway?"); return (ignoreChanges); } return true; }
That was easy, as some would say.
Now the final leaps
Then I decided that it would be simple a matter of using a selector in jQuery and add a function to the change function that sets the isDirty boolean to 'true'.I opted for a class 'trackChanges'.
This is the little snippet that I produced.
$(".trackchange").change(function() { isDirty = true; });
Similarly I chose a class of 'moveaway' for those links that can lead the user away from the current page. These links should get a onclick attribute returning the the result from the IsPageDirty() function described above.
$(".moveaway").attr("onclick", "return IsPageDirty();");
And then there was a lot of work ...
Since I then had to go through the entire form. It was an aspx page and there User Controls (ascx) being loaded and I had to go through thse as well. Not very pleasant.I got myself another cup of coffee and had a good thought. Then I decided to just select all input controls and not just certain marked with a class.
Changed the code to the following.
$("input, select").change(function() { isDirty = true; });Same was true for all the links. I opted for selecting all anchors.
$("a").attr("onclick", "return IsPageDirty();");
Then I had a huge problem, because the saving was also done using a link. Before the saving would start the user gets a warning that there are unsaved changes. A bit confusing to say the least.
I didn't want to go back to putting a class on all the other links just to get around this. I wanted filter out some of the anchors by putting a class on these.
Luckily, jQuery has a function .not() that takes a selector and filters out these from the selection it is applied to. So I put a class 'always' on the anchors that should not have the IsPageDirty() check and changed the line to:
$("a").not(".always").attr("onclick", "return IsPageDirty();");And that was it! A very simple piece of JavaScript code and hardly any changes to the page itself. I only had to add a class to some of the links.
Bringing it all together
Even if it is a very simple scenario and the code is probably not as compact as it can be, I think it shows once again how versatile JavaScript is in combination with library like jQuery.So to get all together
<script type="text/javascript"> var isDirty = false; $(document).ready(function() { isDirty = false; $("input, select").change(function() { isDirty = true; }); $("a").not(".always").attr("onclick", "return IsPageDirty();"); // a with class "always" is not checked }); function IsPageDirty() { if (isDirty) { var ignoreChanges = confirm("Unsaved changes. Proceed anyway?"); return (ignoreChanges); } return true; } </script>
Not earth shocking
I know, I know, but still I think it could be of some help for others somewhere on the web. And maybe to help me remember this for future reference.Small update
The above solution worked on almost all of the pages, but one of the pages had some ASP.NET AJAX stuff on it, where dropdownlist were filled after the initial pageload. This caused some nasty problems, because these made thechange()function go off and set the
isDirtyvariable to true.
Some scratching of the head and then some Googling made find the
live()function to be my saviour in this.
<script type="text/javascript"> var isDirty = false; $(document).ready(function() { isDirty = false; $("input, select").not(".ignore").live("change", (function() { // control with class "ignore" does not fire the isDirty flag isDirty = true; }); $("a").not(".always").attr("onclick", "return IsPageDirty();"); // a with class "always" is not checked }); function IsPageDirty() { if (isDirty) { var ignoreChanges = confirm("Unsaved changes. Proceed anyway?"); return (ignoreChanges); } return true; } </script>
A reader with a good eye also notices the
.not(".ignore")function I added to exclude some controls from firing the isDirty flag. I used this class on some input controls used for search.
That's it for now!
Hi
ReplyDeleteThe isDirty function does not work inside a grid.
Any suggestions??
No reactions without any code to react to. It depends on what kind of "grid" implementation you are referring to.
ReplyDeleteAlso when you hadn't commented as anonymous I probably would have responded sooner.
Hello, this was very helpful for mine uncreativity moment :]
ReplyDeleteThx.