Remove the equal sign in this line:
... it makes the loop run one time too much, hence you get the error.
EDIT:
And this line:
.... should be:
... with the parenthesis, you're calling the function. And if it's being called before the browser has parsed the full page, it's possible that you get this error too.