How to get the focus of an object with Javascript

I thought it would be easy, I found the hasFocus() function, but apparently it only works for the document object. I had to hack this one, so I simply set a global variable to store the focus state of the input field I wanted to monitor, and set the onfocus and onblur handlers for the input field that set the global variable true or false. It’s not clean, but it works like a charm.

In the document’s head:

<script type="text/javascript">
var boxFocusState = false;
</script>

The text box:

<input type="text" id="search" onfocus="boxFocusState=true;" onblur="boxFocusState=false;" />

That’s all it took!