Can element.focus () be used immediately after declaring an element in HTML?

I want to create a form in which I want to automatically indicate the focus on the field. In the old days, I learned to always wait for any DOM manipulation to be performed until the window.onload window is launched, since the DOM may not be ready. Since the JQuery DOMContentReady event has become known, it fires (in compatible browsers) somewhat earlier than window.onload.

This still gives a delay between the moment the field is displayed and the moment it receives focus (using document.getElementById ("input field"). Focus () in window.onload or after DOMContentReady is run). To minimize this delay, some people suggest using focus () immediately after the element on your page, for example:

<form name="f">
  <input id="q" autofocus>
  <script>
    if (!("autofocus" in document.createElement("input"))) {
      document.getElementById("q").focus();
    }
  </script>
  <input type="submit" value="Go">
</form>

(Example from Immersion in HTML5 )

In addition, the Google Closure team advises this practice: "The preferred way is to use the built-in scripts as soon as possible , for example, another answer to SO .

, ( , , , ). : ?. , , , , ( ). .

+3
2

, , () .

:

  • ( DOM, , )
  • DOM ( )

:

  • , .
  • - <script>
+3

, , , , , , . , , . , .

, , , , script node javascript, node, . .

+3

Source: https://habr.com/ru/post/1732381/


All Articles