Tuesday, February 09, 2010

JQuery Autocomplete and the Back Button

JQuery's autocomplete plugin is great. No problems with it so far, easy to use, etc. However, one of my testers just found a problem:

1. Autocomplete is on Page Main. Enter text, choose option. Result function does redirect to page Other.
2. User hits back button on browser. Main shows up again.
3. form focus is still on autocomplete, so hit backspace and ... UG! space is cleared and now browser is trying to autocomplete something else into that field (usernames of all things).

So, after trying several things, including a window.onbeforeunload() method and clearing the autocompleted field, found that problem is solved by CHANGING FOCUS AWAY AND BACK.

Yes, it's that simple. Main's document ready function is called again after the user comes back using back button (thank goodness), so I just put a pair of lines in:


and it was fixed.