Thursday 8 July 2021

Autofocus address bar in a mobile website using JavaScript

Is it possible to autofocus the address bar in a mobile website using JavaScript? (Background: I want to open a site of mine on Chrome Browser for Android and have the address bar focused to be able to type in it)

I found this code example but it only works for an input field within the website:

Homepage: <input type="url" id="myURL" value="http://www.google.com">

<p>Click the buttons below to give focus and/or remove focus from the URL field.</p>

<button type="button" onclick="getFocus()">Get focus</button>
<button type="button" onclick="loseFocus()">Lose focus</button>

<p><strong>Note:</strong> elements with type="url" are not supported in IE 9 (and earlier), or Safari.</p>

<script>
function getFocus() {
  document.getElementById("myURL").focus();
}

function loseFocus() {
  document.getElementById("myURL").blur();
}
</script>


from Autofocus address bar in a mobile website using JavaScript

No comments:

Post a Comment