Tom
Tom

Reputation: 6004

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>

Upvotes: 3

Views: 621

Answers (2)

Alfred.37
Alfred.37

Reputation: 159

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:

Upvotes: 0

Alfred.37
Alfred.37

Reputation: 159

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

Ctrl+l

Upvotes: 1

Related Questions