Focus() event does not work on the browser console

Asked

Viewed 272 times

6

Whenever I want to test something through the web I end up using the console browser (F12 > Console) to check some behaving, especially in the case of . However, I recently noticed that the event .phocus() does not work as expected.

See the example below:

<input id="teste" type="text" id="teste" />

<br/><br/><br/>

<button onclick="document.getElementById('teste').focus()">teste</button>

If I click the button "test", the input will have focus, but if I type the same command that is in the onclick() in the console, nothing happens.

Faced with this, how to do the .focus() function and why this occurs?

Note: This is not just because of the snippet. If I try to do the same with the input Sopt’s research, nothing happens:

document.querySelector('.top-bar .searchbar input[type="text"].f-input').focus();

2 answers

5


I believe it’s something related to "behavioural policies", the same occurs in Firefox as well as in Chrome.

When you are in Devtools (Chrome or Firefox console) the browser tab or window is not in focus and then any <input> or <textarea> will not receive focus, a simple test is to add a timeout in the Console, within the Sopt site itself open the console of your browser and type this:

setTimeout(function () {
    document.querySelector('.top-bar .searchbar input[type="text"].f-input').focus();
}, 5000);

You will have 5 seconds to return to the tab, then click anywhere on the site other than link, input or textarea and once the 5 seconds pass the input you will receive the focus.

Now do another test like this, but instead of leaving the tab in focus and type with setTimeout with 5 seconds again, wait 6 seconds leave the tab in focus, click on the tab and not on the page, you will notice that it will receive the focus too, ie elements "inputables" only receive focus when the tab or page is in focus.

See an example without timeout, if I click on the tab to focus it, but not on the "body" of the page, the input will already be "marked/flagged" to receive the focus when the tab is also in focus:

foco na aba

  • I know it’s not in the question, but is there a specific reason for .click() function and the .focus() no? For example, on the Sopt website we can use this to go to the profile: document.querySelector('.top-bar .my-profile').click();

  • 2

    @Randrade I find fully valid for the subject, since it refers to the policies of behavior, note that .click works in most cases, others will not (like opening certain types of windows), for other common tasks the click is something that can be programmable because the working policy probably understands that it will not affect the user directly, that is to be an action inside the page and will not control the "mouse/mouse", since Focus would probably control the pointer, and what was typed would enter the field. I can’t say for certain, but I promise to bring sources ;)

4

Because this is the way the browser works on the console (so much so that it ignores and returns Undefined®).

inserir a descrição da imagem aqui

Whenever you run a command on the console, the focus will always be on the command line, waiting for the next command, ignoring any focus pointing to any element on the page.

However, if you run the command below and immediately click on any part of the page, the focus will work:

setTimeout(function(){document.getElementById('teste').focus()},5000);

Why?

Because you took the console focus and the command will run normally after 5 seconds.


Test here at Sopt:

Run the command below on the console (F12) and immediately click on any area of the page (blank, empty area) or close the console:

setTimeout(function(){document.querySelector('#search > input').focus();}, 5000);
  • I know it’s not in the question, but is there a specific reason for .click() function and the .focus() no? For example, on the Sopt website we can use this to go to the profile: document.querySelector('.top-bar .my-profile').click();

  • @Randrade The reason is in the answer. The console does not release the focus to anything, unless you click off it. The click does not depend on focus, it is a trigger that the browser runs normally.

  • Do not ignore and Undefined has nothing to do with this, it returns Undefined because the function .focus() does not return anything even if it was "boolean" would probably return something, but it is not. Other, it is not ignored, the element is flagged with focus, so much so that if you click on the tab after executing the command on the console you will notice that the focus occurs on the input at the time the tab received the focus too, even if long after, it will only lose focus if you click inside the tab, ie anywhere on the page, see the GIF I posted for testing (cc @Randrade)

  • 2

    @Guilhermenascimento Blz. I caricatured the Undefined thing. Your answer is excellent, so much so that I even gave +1 on it. Good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.