Thought of sharing some edge cases for this subject.
If your content is reloading (example dynamic DOM loading results from API and setting focus on first item of results) adding attribute autofocus will not be your solution, it works only on first load, second DOM change will not work but works fine in static DOM or single page load. If you have Dynamic component loading data simple .focus() will fail due to triggering focus on element not created yet by the time focus() is or blur not complete yet by DOM. For this case expected is to add delay time (setTimeout function) to give a time for focus to apply to new created or recreated element in DOM. My case was to load data from API and get focus on first result.
Adding
//focusId is ID of element you need focus on
var el = document.getElementById(focusId);
el.focus();
solved the issue so DOM completes blur without adding delay.
document.getElementById('your_text_box_id').focus();.autofocuson the html input tag is often enough.