JavaScript: Auto Focus, onLoad

by MythOfEchelon

HTML

<!DOCTYPE html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    
    <body>
        <input type="text" id="textboxName" />
        
        <!-- Execute the script after all DOM elements have been created -->
        <script type="text/javascript">
            window.onload = setFocus();
            
            function setFocus(){
                var textboxElement = document.getElementById("textboxName");
                textboxElement.focus();
                
                /*
                  The variable is not necessary. You can just do:
                  "document.getElementById("textboxName").focus();"
                  but it is good practise to use variables
                */
            }
        </script>
    </body>
</html>