Mobile Safari auto focus on text input

focusmobile-safarisafari

I would like to focus on a text input field immediately on page load. However the following code, that works on desktop browsers, doesn't work on mobile Safari (on iOS 6).:

  <input type="text" name="q" class="search" id="focusme" />

  ...

  <script type="text/javascript">
    window.onload=function(){var a=document.getElementById("focusme");a.focus();}
  </script>

I have tried to implement several suggested fixes for this. However I haven't been able to get it working. It does display the CSS changes to the field when it is in focus, however the keyboard is not brought up.

I suspect this may be due to my currently very limited understanding of JavaScript, but I think it may still be a bug in Safari.

The question:

In Safari on iOS 6, Do you know of any way to auto focus on a text input field on page load?

Thank you for your help 🙂

Update:

The page does have jquery and mobile-jquery available, if they need to be used.

Best Answer

Why using JavaScript? <input type="text" autofocus /> should do it!