tags:

views:

43

answers:

3

When you refresh/reload a page or use the back button, Firefox is kind enough to repopulate your inputs with what was entered before you navigated away.

Though this is a nice feature it does not trigger my jquery validation and the unsaved changes warning I add to my pages.

Is there a way to either disable this feature in Firefox (without renaming every control every time) or capture the firefox events?

A: 

You can use the trigger() method to trigger any events (custom or not).

Suposing you have a validate event :

$(document).ready(function()
    {
        $('#yourform').trigger('validate');
    }
);

If your validation plugin makes use of a function to validate, then simply do :

$(document).ready(function()
    {
          validate($('#yourform'));
    }
);

which will be triggered when your document is ready.

Eventualy use setTimeout to avoid loading time during the way to browser load the cached input values.

Boris Guéry
I'm using the jquery validate plugin and I'm already doing the call to validate in the document ready. I don't believe firefox is triggering the events that make validate run. It's as if firefox is overriding the defaults instead
Chris Simpson
Use console.log($('#youform input').val()) to see which values are retrieved by your code.
Boris Guéry
A: 

Could I suggest you add a delayed function to validate and check for form content a few seconds after page load? That would probably be the safest way to catch all browsers, some of which only insert saved form data once the whole page has finished loading.

Nicholas Wilson
A delayed check would have to be my very last resort. It's a dangerous road to go down. Also, if firefox is not triggering the changed event, how do I know what's changed?
Chris Simpson
+1  A: 

After your .validate() call in document.ready, also call .valid() once to trigger validation as well, like this:

$(function() {
  $('#myForm').validate({ /* options here */ });
  $('#myForm').valid();
});
Nick Craver
Thanks, I'll give that a try.
Chris Simpson
this does work for the validate (thank you) but it only tells me the page is not valid rather than something has changed. I need this to inform the user there are unsaved changes and warn on navigating away. Any way to tell that what is being display is not what was pulled from the server?
Chris Simpson
@Chris - Do you want them to have cached input? It sounds like you *might* want to prevent the page from caching all-together...if that's the case post which server-side framework you're using, as the answer varies.
Nick Craver
@Nick - I can see how the cached input would be useful but the simplest behaviour would be the no-caching option. However, I understand it's difficult to disable this behaviour in firefox? Therefore I was looking at how I can work with it.
Chris Simpson
@Chris - It's pretty easy in every browser, what server platform are you using? All current browsers (even IE6) will respect the no caching headers, so I'd definitely go with that option.
Nick Craver
This will be IIS6 and IIS7. Is it best to put these headers in IIS, the response or as a meta tag on the page?
Chris Simpson
@Chris - No server-side ASP.Net or anything, all static files served by IIS?
Nick Craver
This is an ASP.Net MVC site so, yeah, there's a tonne of server side magic. I was just wondering where the best place for the caching option was
Chris Simpson
@Chris - There are a few methods you can do this by, I'd take a look at this question: http://stackoverflow.com/questions/1160105/asp-net-mvc-disable-browser-cache and this question: http://stackoverflow.com/questions/860678/how-can-i-disable-client-side-and-proxy-caching-in-asp-net-mvc
Nick Craver