I am trying to set focus using javascript on the first unfocused input element of MVC view in a load window event listener. But element is focused only after page reloading. Attached browser - MS Edge I tried reloading the page programmatically using window.location.reload, but it didn't have the same effect. Using setTimeout also was not helpful. This only works when previewing the page in Edge from Visual Studio. How to set focus correctly?
Test sample repo: https://github.com/pnfstas/TestFocus
HomeController:
using Microsoft.AspNetCore.Mvc;
namespace TestFocus.Controllers
{
public class HomeController : Controller
{
public IActionResult Start()
{
return View("Start");
}
}
}
start.js:

Start.cshtml:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<form action="javascript:void(0);" method="post" style="width:500px;height:300px;display:flex;flex-direction:column;align-items:stretch;justify-content:space-between;">
<fieldset style="flex:auto;display:flex;flex-direction:column;justify-content:space-evenly;">
<legend>Authentication</legend>
<label id="username-label" for="UserName">User name:</label>
<input class="user-property-input" id="UserName" name="UserName" type="text" />
<label id="email-label" for="Email">E-Mail:</label>
<input class="user-property-input" id="Email" name="Email" type="email" />
<label id="phone-label" for="PhoneNumber">Phone number:</label>
<input class="user-property-input" id="PhoneNumber" name="PhoneNumber" type="tel" />
</fieldset>
<span id="test-focus-span" style="margin-top:15px;"></span>
</form>
<script id="start" src="~/src/start.js" asp-append-version="true"></script>
</body>
</html>