You should probably look through your code again since you are doing lots of pointless things.
e.g. (there are more)
someVar = someVar; // does nothing
// remove the function argument
function f(arg) {
// code that never uses the argument arg
}
Indent your code
// works, but is not readable.
function f(a, b, c){return (a>b&&a>c?'a':(b>c?'b':(c?'c':0)));}
Use names for variables, classes, functions and constants that carries information. function func() makes little sense, since you can tell by syntax that it is a function. function doStuff is equally bad. function setFocusAndWhat would make sense, as soon as you switch out what for whatever is supposed to happen on the last line of code. And once you put that into words, perhaps you will know what code to write as well.
Other than that, why bother setting the caret position to where it used to be? The user submitted data, and there is of course a chance they'd want to edit something. But what is the probability they'd want to edit text in the same position they had the caret when submitting the form?
Anyhow, the solution would be to listen for the event which fires before the control loses focus. onchange is one such event, but it only fires if something changed. Check the reference I provided a link for. You'll find all events there.
What you do is
- save the caret position before losing focus
- make sure this is sent along with the other post data on form submit
- have your server send this data back when the page loads
- use it instead of the last character position when setting focus to your control.