title: How to get mobile browsers (webkit in iOS, Android) to display their soft-keyboards
body: <p>I'm writing a small web app that makes heavy use of the canvas element to do something similar to VNC. It is especially targeted for the default browsers of iOS and Android.</p>
  
  <p>I can detect via AJAX calls when keyboard input is required, but I'm having trouble convincing the mobile browsers to display their soft keyboards on demand. My thought was to have an input field, perhaps hiding behind the canvas, that I could give focus to when appropriate, and then listen for key events on that input field.</p>
  
  <p>The problem is that this doesn't seem to work when it comes "from the background", i.e. on the success handler of an AJAX call. Here is a self-contained test case (requires jQuery, and makes use of a dummy ajax handler mounted at <code>/focus</code>) that shows how the focus doesn't pop up the mobile browser's keyboard when coming from a <code>setTimeout</code> delay or from the return of an AJAX call. Is there any way to get the keyboard to pop-up effectively?</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Focus Test V1&lt;/title&gt;
  &lt;/head&gt;
  &lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt;
  &lt;body&gt;
  
  &lt;div&gt;
  &lt;form method="GET" onsubmit='return false;'&gt;
  &lt;input type="text" id="kb" /&gt;
  &lt;/form&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
  &lt;button id="focus-direct"&gt;Focus Directly&lt;/button&gt;
  &lt;button id="focus-timeout""&gt;Focus Timeout&lt;/button&gt;
  &lt;button id="focus-ajax"&gt;Focus Ajax&lt;/button&gt;
  &lt;button id="clear"&gt;Clear&lt;/button&gt;
  &lt;/div&gt;
  
  &lt;div id="console"&gt;&lt;/div&gt;
  
  &lt;script type="text/javascript"&gt;
      function init() {
          function append(text) {
              $('&lt;div/&gt;').text(text).appendTo('#console');
          }
          $('#clear').click(function() {
              $('#console').html("");
          });
          $('#focus-direct').click(function() {
              focusKB();
          });
          $('#focus-timeout').click(function() {
              setTimeout(focusKB(), 50);
          });
          $('#focus-ajax').click(function() {
              append("sending...");
              $.ajax( {
                  url : '/focus',
                  success : function() {
                      append("ajax returned...");
                      focusKB();
                  }
              });
          });
  
          function focusKB() {
              $('#kb').focus();
              append("focusing...");
          }
  
          setTimeout(focusKB, 3000);
      }
      $(document).ready(init);
  &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>