3

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>
Tom
  • 4,875
  • 18
  • 69
  • 119

2 Answers2

1

You can send the follow shortcut by js or by other prefered programming language to the browser to get focus on adressbar:

Ctrl+l
Anna23
  • 11
  • 2
0

Based on answer of Anna23:

you can do it on mac too by sending the follow shortcut:

Command + L

A javascript sample code for sending a shortcut to a browser, you find on follow link:

Eddy763
  • 27
  • 10