How can I find out if a document has focus in chrome developer console

1

I am trying to see if my document has focus using this API.

https://developer.mozilla.org/en-US/docs/Web/API/Document/hasFocus

So what I am doing is load my page and the in Chrome developer console, I type 'document.hasFocus()'

The problem is it always return false.that is because after I load the page, i need to click on the chrome developer console before I can type 'document.hasFocus()' and execute that method.

So that gives the console focus.

How can I work around this problem?

Thank you.

n179911

Posted 2015-11-25T20:42:47.300

Reputation: 3 123

How exactly is this a problem? You are checking if your document has focus when it doesn't have focus and correctly being told that it does not. Do you want a function that tells you that your document has focus when it doesn't? – David Schwartz – 2015-11-25T20:49:17.027

I think since i move the focus away from the document by click on the chrome developer console, so the document will always has no focus in my debugging. – n179911 – 2015-11-25T21:01:12.607

Right. So why is that a problem? It doesn't have focus, and it knows it doesn't have focus. Do you want some way to tell the app it has focus when it doesn't? If so, write your own hasFocus function that overrides the test if some other parameter is set and call that. – David Schwartz – 2015-11-25T21:22:39.990

Answers

0

Instead of running the command from the console, create a button on your page. When clicked, logged the output of the method.

<button type=button onclick="function() {console.log(document.hasFocus());}">Do I have focus?</button>

Garbee

Posted 2015-11-25T20:42:47.300

Reputation: 116