0

Today I run into something unexpected when writing JavaScript in Chrome (version 37). It appears Chrome has a predefined focus variable available as a global. Upon further inspection via developer tools, I've found that this variable is a function:

> typeof focus
"function"

Does anyone know what this function is or does, or why is it a global?

I had a variable named focus and this global messed everything up in my webapp. I spent the last hour debugging where focus was getting created and it turned out that Chrome had it predefined.

bodacydo
  • 75,521
  • 93
  • 229
  • 319

1 Answers1

1

You are talking about

window.focus

Makes a request to bring the window to the front. It may fail due to user settings and the window isn't guaranteed to be frontmost before this method returns.

It is a built-in function, not a reserved word but like open not a good name for a variable. Other baddie is naming your submit button submit and then trying to submit the form programatically

Any variable defined in global scope may mess with other window.xxxx vars/functions.

More window functions.

mplungjan
  • 169,008
  • 28
  • 173
  • 236